Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is jquery used to check for the 'disabled' attribute on an <a> tag?

Tags:

html

jquery

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'); 
like image 241
coder Avatar asked Apr 17 '12 20:04

coder


2 Answers

Try

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

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

like image 154
bardiir Avatar answered Oct 12 '22 02:10

bardiir


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.

like image 20
Ayman Safadi Avatar answered Oct 12 '22 00:10

Ayman Safadi