37

I have a disabled='disabled' attribute on an <a> tag. How do I test to see if this attribute is on my tag using jquery? The following code returns undefined. I can see the disabled attribute on the tag in firebug and all other attributes on the anchor return successfully using the same syntax. I realize disabled is a custom attribute for an <a> tag.

$('#anchorID').attr('disabled');
1
  • 1
    I'm not adding disabled, I just need to check for it's presence.
    – coder
    Apr 17, 2012 at 20:21

4 Answers 4

60

Try

$('#anchorID').is('[disabled=disabled]')

Will return true if disabled="disabled" is an attribute for the element.

1
  • This is not always reliable, use .prop() (see other answers)
    – dev_masta
    Sep 27, 2019 at 19:21
53

The new and improved way is to use jQuery's prop() function: http://api.jquery.com/prop/#prop1

$('#anchorID').prop("disabled");

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

2
  • 3
    This is a very informative answer but it did not return the correct value for my field.
    – coder
    Apr 17, 2012 at 20:29
  • 2
    This doesn't actually work on <a> tags. It's good for <input> and <button> tags though. @bardiir's answer is better.
    – Ryan
    Aug 4, 2016 at 2:36
11

Try:

$('#anchorID').prop("disabled");

See:

http://api.jquery.com/prop/#prop1

Updated: +1 to Ayman Safadi's answer.

1
  • Works for 'checked' not for 'disabled' Dec 28, 2015 at 17:00
1

An JS alternative:

let hasDisabledAttr =  document.querySelector('#anchorID').hasAttribute('disabled');

Return true or false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.