How can I add readonly
to a specific <input>
? .attr('readonly')
does not work.
Use setAttribute() Method to add the readonly attribute to the form input field using JavaScript. setAttribute() Method: This method adds the defined attribute to an element, and gives it the defined value. If the specified attribute already present, then the value is being set or changed.
The readonly attribute is a boolean attribute. When present, it specifies that an input field is read-only. A read-only input field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).
The :read-only selector selects elements which are "readonly". Form elements with a "readonly" attribute are defined as "readonly".
The readonly attribute of <input> element in HTML is used to specify that the input field is read-only. If an input is readonly, then it's content cannot be changed but can be copied and highlighted.
jQuery <1.9
$('#inputId').attr('readonly', true);
jQuery 1.9+
$('#inputId').prop('readonly', true);
Read more about difference between prop and attr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With