Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a “readonly” attribute to an <input>?

How can I add readonly to a specific <input>? .attr('readonly') does not work.

like image 420
Qiao Avatar asked Aug 20 '09 14:08

Qiao


People also ask

How do you add a readonly attribute?

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.

How do I make input type readonly?

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).

How do you add a readonly attribute in CSS?

The :read-only selector selects elements which are "readonly". Form elements with a "readonly" attribute are defined as "readonly".

How do you make an input field read only in HTML?

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.


1 Answers

jQuery <1.9

$('#inputId').attr('readonly', true); 

jQuery 1.9+

$('#inputId').prop('readonly', true); 

Read more about difference between prop and attr

like image 77
Christian C. Salvadó Avatar answered Sep 17 '22 15:09

Christian C. Salvadó