Adding attribute in jQuery
How can I add an attribute into specific HTML tags in jQuery?
For example, like this simple HTML:
<input id="someid" />
Then adding an attribute disabled="true" like this:
<input id="someid" disabled="true" />
You can add attributes using attr
like so:
$('#someid').attr('name', 'value');
However, for DOM properties like checked
, disabled
and readonly
, the proper way to do this (as of JQuery 1.6) is to use prop
.
$('#someid').prop('disabled', true);
best solution: from jQuery v1.6 you can use prop() to add a property
$('#someid').prop('disabled', true);
to remove it, use removeProp()
$('#someid').removeProp('disabled');
Reference
Also note that the .removeProp() method should not be used to set these properties to false. Once a native property is removed, it cannot be added again. See .removeProp() for more information.