How to append an item in a if-statement just once?

Viewed 108

So every time I click the button to submit the form, the appended div appends itself over. How can I make it so the div appends itself just once?

<div class="field">
        <div class="field">
            <div id='eerr' class="errors"></div>
        </div>
</div>

if (email == '' || emailResult == false) {
    $('#eerr').append('<div class="ui basic red pointing prompt label transition visible">Please insert your email.</div></div>');
    }

Btw, this is the button:

<button style="margin-top: 7px" type="button" onclick='singup()' class="ui primary submit button login ">clickme</button>
1 Answers

Since this is for form validation, it sounds like you want the #eerr to contain all the warnings. There may be additional checks and warnings you want to add, so you might just clear the #eerr at the beginning of validation:

const $errorElm = $('#eerr');
$errorElm.empty();
if (email == '' || emailResult == false) {
    $errorElm.append('<div class="ui basic red pointing prompt label transition visible">Please insert your email.</div></div>');
}
// other validation involving $errorElm.append
Related