views:

974

answers:

1

Consider the following code:

$("a").attr("disabled", "disabled");

In IE and FF, this will make anchors unclickable, but in WebKit based browsers (Google Chrome and Safari) this does nothing. The nice thing about the disabled attribute is that it is easily removed and does not effect the href and onclick attributes.

Do you have any suggestions on how to get the desired result. Answers must be:

  • Easily be revertable, since I want to disable form input controls while I have an AJAX call running.
  • Must work in IE, FF, and WebKit
+5  A: 

I assume that you have an onclick event handler bound to these anchor elements. Just have your event handler check the "disabled" attribute and cancel the event if it is set. Your event handler would look something like this:

$("a").click(function(event){
  if (this.disabled) {
    event.preventDefault();
  } else {
    // make your AJAX call or whatever else you want
  }
});

You can also set a stylesheet rule to change the cursor.

a[disabled=disabled] { cursor: wait; }

Edit - simplified the "disabled" check as suggested in comments.

Neall
function diableForms() { $("input, button, textarea, a").attr("disabled", "disabled").css("cursor", "wait"); } function enableForms() { $("input, button, textarea, a").not(".disabled").removeAttr("disabled").css("cursor", ""); }
Stefan Rusek
I just pasted the disableForms and enableForms functions I am using. Thanks for your suggestion.
Stefan Rusek
if(this.disabled == "disabled") will be quicker
Sugendran
actually I think it's if(this.disabled) - i really should check my comments before i write them
Sugendran
if(this.disabled) is what I used. I don't have enough karma to edit posts or I would add some of this too Neall's post
Stefan Rusek