I have the following input:
<input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname"/>
How can I use jQuery to make this element a read-only input without changing the element or its value?
I have the following input:
<input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname"/>
How can I use jQuery to make this element a read-only input without changing the element or its value?
There are two attributes, namely readonly and disabled, that can make a semi-read-only input. But there is a tiny difference between them.
<input type="text" readonly />
<input type="text" disabled />
readonly attribute makes your input text disabled, and users are not able to change it anymore.disabled attribute make your input-text disabled(unchangeable) but also cannot it be submitted.jQuery approach (1):
$("#inputID").prop("readonly", true);
$("#inputID").prop("disabled", true);
jQuery approach (2):
$("#inputID").attr("readonly","readonly");
$("#inputID").attr("disabled", "disabled");
JavaScript approach:
document.getElementById("inputID").readOnly = true;
document.getElementById("inputID").disabled = true;
PS prop introduced with jQuery 1.6.
Given -
<input name="foo" type="text" value="foo" readonly />
this works - (jquery 1.7.1)
$('input[name="foo"]').prop('readonly', true);
tested with readonly and readOnly - both worked.