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" />
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);
$('#someid').prop('disabled', true); doesn't work, but $('#someid').attr('disabled', true); works fine.attr or prop, it's important to understand the difference between properties and attributes. Without understanding the difference, you're likely to run into unexpected behavior. See stackoverflow.com/a/6004028/3367343best 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.
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");
Use this code:
<script>
$('#someid').attr('disabled', 'true');
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Text inside $ symbol select all element with id = someid , disabled attribute of this id change to trueThis could be more helpfull....
$("element").prop("id", "modifiedId");
//for boolean
$("element").prop("disabled", true);
//also you can remove attribute
$('#someid').removeProp('disabled');
.attr() to set or read it. See the paragraph "Attributes vs. Properties" under .prop() and .attr() in the jQuery docs. Also see Paul Rosania's answer above.If you are trying to use removeProp() for "checked", "disabled" or "selected", it might not work. To remove property use prop with 'false' parameter.
$('.my-input').prop('disabled', true); //adding property
$('.my-input').prop('disabled', false); //removing property
documentation: https://api.jquery.com/removeprop/