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

JavascriptJquery

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

jQuery <1.9

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

jQuery 1.9+

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

Read more about difference between prop and attr

Solution 2 - Javascript

Use $.prop()

$("#descrip").prop("readonly",true);

$("#descrip").prop("readonly",false);

Solution 3 - Javascript

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!

Solution 4 - Javascript

.attr('readonly', 'readonly') should do the trick. Your .attr('readonly') only returns the value, it doesn't set one.

Solution 5 - Javascript

I think "disabled" excludes the input from being sent on the POST

Solution 6 - Javascript

You can disable the readonly by using the .removeAttr;

$('#descrip').removeAttr('readonly');

Solution 7 - Javascript

For enabling readonly:

$("#descrip").attr("readonly","true");

For disabling readonly

$("#descrip").attr("readonly","");

Solution 8 - Javascript

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>

Solution 9 - Javascript

Check the code below:

<input id="mail">

<script>

 document.getElementById('mail').readOnly = true; // makes input readonline
 document.getElementById('mail').readOnly = false; // makes input writeable again

</script>

Solution 10 - Javascript

For jQuery version < 1.9:

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

For jQuery version >= 1.9:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQiaoView Question on Stackoverflow
Solution 1 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - JavascriptGregView Answer on Stackoverflow
Solution 3 - Javascriptuser1310388View Answer on Stackoverflow
Solution 4 - JavascriptceejayozView Answer on Stackoverflow
Solution 5 - JavascriptPierView Answer on Stackoverflow
Solution 6 - JavascriptSteveView Answer on Stackoverflow
Solution 7 - JavascriptPradeepView Answer on Stackoverflow
Solution 8 - JavascriptDiogo RodriguesView Answer on Stackoverflow
Solution 9 - JavascriptwarpiView Answer on Stackoverflow
Solution 10 - JavascriptOsman AdakView Answer on Stackoverflow