Nov
23
2011

Avoid double clicking with jQuery

There has been always one troubling issue regarding web forms and user interaction: when the user double clicks the submit button. Detecting and preventing it or disabling the button is an expensive operation in terms of computing. Fortunately, jQuery has a neat solution. As usual.

Enter one function. This function will simply unbind the click event from an object after it has been clicked. It is really that simple. All you need to pass to one() is the event that you want to bind/unbind and a handler function to execute when the event is triggered:


$('#submit-form').one('click', function() {
alert('Hang on!');
});


The code for a simple example doing this is:

$(document).ready(function(){
$('#my_DIV_ID').one('click', function() {
$(this).slideUp();
});
});


More about one() in the documentation for jQuery.

About the Author: SGH

At the same time, I can’t think of anything more satisfying than sitting in front of TV, eating take-away, lights down low, in my pyjamas and watching crap on TV. Basically, I strive to live a balanced life. Fun. Serious. Fast. Slow. Brainy. Bimbotic. Intense. Lazy. You get the drift.

Leave a comment

Click to refresh

Subscribe to newsletter

Sign up with your email to get updates about new resources releases and special offers.