Reputation: 13
What I am trying to do is to append text to .showError
only once every time you click on the button. Right now it appends each time you click on the button and get multiple times text appended to .showError
. How do I prevent this from happening?
$("#send").on("click", function(ev) {
$(".error-holder").empty();
var err = 0;
$("form .form-control").each(function(i, v) {
if ($(this).val() == "") {
err = 1;
}
});
if (err == 1) {
ev.preventDefault();
$(".showError").append("Bij dit product moeten er serienummers ingevuld worden! <br /><br />");
($('#myModal').modal());
}
});
Upvotes: 1
Views: 1722
Reputation: 3830
Use .html()
:
$("#send").on("click", function(ev) {
$(".error-holder").empty();
var err = 0;
$("form .form-control").each(function (i,v) {
if ($(this).val() == "") {
err = 1;
}
});
if(err == 1){
ev.preventDefault();
$(".showError").html("Bij dit product moeten er serienummers ingevuld worden! <br /><br />");
($('#myModal').modal());
}
Upvotes: 2
Reputation: 46900
Add a new variable before you start checking, let's call it
var showError=1;
Then go to your loop which checks for all the values and when you reach this stage
if(err == 1){
Edit it to like
if(err == 1){
{
if(showError== 1)
{
// Your append line here
showError=0;
}
//Your Modal code here
}
This way error will be shown only once because after that you are updating your variable to tell your if condition to fail.
Upvotes: 2
Reputation: 32354
Try something like this:
var appended = 0;
$("#send").on("click", function(ev) {
$(".error-holder").empty();
var err = 0;
$("form .form-control").each(function (i,v) {
if ($(this).val() == "") {
err = 1;
}
});
if(err == 1){
ev.preventDefault();
if (appended == 0) {
$(".showError").append("Bij dit product moeten er serienummers ingevuld worden! <br /><br />");
appended = 1;}
($('#myModal').modal());
}
Upvotes: 1