Disabling links to stop double-clicks in JQuery

Viewed 58734

How would I disable all links with the button class after they are clicked once? I'd like to be able to do this in one place, and not have to change all of them individually.. any ideas?

So far I got this:

$("a.button").click(function() { $(this).attr("disabled", "disabled"); });
$("a[disabled]").click(function() { return false; });

but the second event isn't firing..

16 Answers

I have an elegant solution for this if you're using links:

function do_nothing() { 
  return false;
}

// prevent a second click for 10 seconds. :)
$('.prevent_doubleclick').live('click', function(e) { 
  $(e.target).click(do_nothing); 
  setTimeout(function(){
    $(e.target).unbind('click', do_nothing);
  }, 10000); 
});

https://github.com/ssoroka/prevent_doubleclick.js

I'll add support for forms soon.

You always make a big hassle over a sipmple thing.

Set delayer self resetting variable when first click. That disables second clicks over some time. Simple!

    var formvar=1;

$('#myForm').submit(function() { 
    if(formvar==1) 
        {
        $(this).ajaxSubmit(options); // or do what ever normally do

        setTimeout(function(){formi=1}, 3000);
        formvar=0;
        }
    else alert("no dbl clicking");


    }); 
Related