Adding attribute in jQuery
JqueryHtmlTagsJquery Problem Overview
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" />
Jquery Solutions
Solution 1 - Jquery
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);
Solution 2 - Jquery
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');
> 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.
Solution 3 - Jquery
You can do this with jQuery's .attr
function, which will set attributes. Removing them is done via the .removeAttr
function.
//.attr()
$("element").attr("id", "newId");
$("element").attr("disabled", true);
//.removeAttr()
$("element").removeAttr("id");
$("element").removeAttr("disabled");
Solution 4 - Jquery
$('#someid').attr('disabled', 'true');
Solution 5 - Jquery
$('#someid').attr('disabled', 'true');
Solution 6 - Jquery
Add attribute as:
$('#Selector_id').attr('disabled',true);
Solution 7 - Jquery
$('.some_selector').attr('disabled', true);
Solution 8 - Jquery
Use this code:
<script>
$('#someid').attr('disabled', 'true');
</script>
Solution 9 - Jquery
This could be more helpfull....
$("element").prop("id", "modifiedId");
//for boolean
$("element").prop("disabled", true);
//also you can remove attribute
$('#someid').removeProp('disabled');
Solution 10 - Jquery
$('#yourid').prop('disabled', true);