How can I add readonly to a specific <input>? .attr('readonly') does not work.
-
2.attr('readonly', true) works, other dontQiao– Qiao2009-08-20 14:54:29 +00:00Commented Aug 20, 2009 at 14:54
-
3.attr('readonly', 'readonly') also worksQiao– Qiao2009-08-20 14:56:36 +00:00Commented Aug 20, 2009 at 14:56
-
3It depends on which DOCTYPE do you use. For HTML 4.01 DTD's the answer from CMS is working and valid HTML 4.01. If you use a XHTML DTD then the answer from ceejayoz is working and valid XHTML.bjoernwibben– bjoernwibben2009-08-20 15:01:02 +00:00Commented Aug 20, 2009 at 15:01
-
2It doesn't depend on doctype, the DOM is the same whether it was read in through HTML or XML, and in any case XHTML is almost always still actually served as HTML not XML, for IE compatibility.bobince– bobince2009-09-11 16:44:56 +00:00Commented Sep 11, 2009 at 16:44
10 Answers
jQuery <1.9
$('#inputId').attr('readonly', true);
jQuery 1.9+
$('#inputId').prop('readonly', true);
Read more about difference between prop and attr
3 Comments
Use $.prop()
$("#descrip").prop("readonly",true);
$("#descrip").prop("readonly",false);
3 Comments
$.prop(): Prop will set the readonly attribute to blank/empty string, so if you have have any CSS that uses the attribute selector for [readonly="readonly"], then you'll have to change this to [readonly] (or include both).Readonly is an attribute as defined in html, so treat it like one.
You need to have something like readonly="readonly" in the object you are working with if you want it not to be editable. And if you want it to be editable again you won't have something like readonly='' (this is not standard if I understood correctly). You really need to remove the attribute as a whole.
As such, while using jquery adding it and removing it is what makes sense.
Set something readonly:
$("#someId").attr('readonly', 'readonly');
Remove readonly:
$("#someId").removeAttr('readonly');
This was the only alternative that really worked for me. Hope it helps!
Comments
.attr('readonly', 'readonly') should do the trick. Your .attr('readonly') only returns the value, it doesn't set one.
2 Comments
.attr value should only be a string or number, not a boolean, according to the jQuery docs: api.jquery.com/attr/#attr2 Also, HTML "boolean attributes" should only be empty string or the value of the attribute. I think the solution is to use .prop() to avoid confusion.I think "disabled" excludes the input from being sent on the POST
3 Comments
Use the setAttribute property. Note in example that if select 1 apply the readonly attribute on textbox, otherwise remove the attribute readonly.
http://jsfiddle.net/baqxz7ym/2/
document.getElementById("box1").onchange = function(){
if(document.getElementById("box1").value == 1) {
document.getElementById("codigo").setAttribute("readonly", true);
} else {
document.getElementById("codigo").removeAttribute("readonly");
}
};
<input type="text" name="codigo" id="codigo"/>
<select id="box1">
<option value="0" >0</option>
<option value="1" >1</option>
<option value="2" >2</option>
</select>
Comments
For enabling readonly:
$("#descrip").attr("readonly","true");
For disabling readonly
$("#descrip").attr("readonly","");