Reputation: 21854
When I submit my form, I display a message in jQuery.
I want it appears only once when I submit more than once
$('form').submit(function(){
if ($('#title').val() != '' && $('#comment').val() != '')
$('form').append($('<span>').text('Bug sent').delay(4000).fadeOut(600));
else
$('form').append($('<span>').text('Fields are empty').delay(4000).fadeOut(600));
return false;
});
Example : http://jsfiddle.net/bUS8e/
Upvotes: 1
Views: 1100
Reputation: 1379
You should clear the parent element before:
$('form').submit(function(){
if ($('#title').val() != '' && $('#comment').val() != '')
$('form').empty().append($('<span>').text('Bug sent').delay(4000).fadeOut(600));
else
$('form').empty().append($('<span>').text('Fields are empty').delay(4000).fadeOut(600));
return false;
});
Upvotes: 0
Reputation: 56477
Add a div
(or any other element) to your form and use $('div').html([YOUR STUFF])
instead of $('form').append([YOUR STUFF])
.
Upvotes: 1
Reputation: 272136
Remove all spans when the user submits the form. Then add them again (if necessary):
Upvotes: 2
Reputation: 9311
You could add the span
to the HTML code and give it an ID. And instead of appending a new span
everytime, you just reuse the existing one by addressing it via its ID.
Upvotes: 1
Reputation: 6522
Check if $('form').text()
contains "Bug sent"?
i.e.
$('form').submit(function(){
if ($('#title').val() != '' && $('#comment').val() != '')
if (!/Bug sent/.test($('form').text()) {
$('form').append($('<span>').text('Bug sent').delay(4000).fadeOut(600));
}
else
$('form').append($('<span>').text('Fields are empty').delay(4000).fadeOut(600));
return false;
});
Upvotes: 2
Reputation: 154848
Simply remove any existing span
s in the form: http://jsfiddle.net/pimvdb/bUS8e/1/.
$('form span').remove();
Note that this would also remove any other, irrelevant span
s which you might not want to have removed. In that case, you can add a class to the message span
s you add, and use $('form span.message').remove();
so that only those message span
s get removed.
Upvotes: 3