Reputation: 1507
I have a button or anchor element that redirect the user to a different view. Once the button is clicked, it fires a DB write event. However, when the user clicks the button multiple times waiting for the page to reload, multiple records get created and it causes issues with the application saving the form that follows.
What is the best way, maybe in javascript or jQuery to ignore the clicks after the first one?
UPDATE:
I actually did this and it worked -- it doesn't disable button but returns false on the on click event.
<script>
$("#linkresponse").click(function() {
$(this).click(function() {
return false;
});
return true;
});
</script>
Does this seem like a good solution, any reason I shouldn't follow it?
Upvotes: 1
Views: 1739
Reputation: 1507
I actually added this script. It does not disable the button, but it will return false for each subsequent click. It works now! Thank you for your suggestions as well.
<script>
$("#buttonid").click(function() {
$(this).click(function() {
return false;
});
return true;
});
</script>
Upvotes: 0
Reputation: 35680
Whenever an event should occur only once for an element, use jQuery's one()
method:
$('#myButton').one('click', function(){
...
});
Upvotes: 5
Reputation: 3330
Create a global variable (call it hasClicked or something) set to 'false'. In your onclick method, have logic like this at the very top:
if (hasClicked)
return;
else
hasClicked = true;
Upvotes: 0