Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling links to stop double-clicks in JQuery

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..

like image 689
cloudhead Avatar asked Nov 05 '09 16:11

cloudhead


People also ask

How do I stop my keys from double clicking?

Present Code click(function (e) { // Prevent button from double click var isPageValid = Page_ClientValidate(); if (isPageValid) { if (isOperationInProgress == noIndicator) { isOperationInProgress = yesIndicator; } else { e.

How do I disable a link in HTML?

It is still possible to disable a link by following 3 steps: remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.


1 Answers

This is what I would try:

$("a.button").one("click", function() {     $(this).click(function () { return false; }); }); 

Bind all the links with class "button". Run the anonymous function only once (hence "one"), which rebinds the link to return false.

If you want to keep it looking more like what you have, try this:

$("a.button").click(function() { $(this).attr("disabled", "disabled"); }); $(document).click(function(evt) {      if ($(evt.target).is("a[disabled]"))           return false; }); 
like image 198
aakoch Avatar answered Sep 21 '22 13:09

aakoch