jQuery Read-Only Input Field

Share this article

Simple jQuery code snippet to make an input field read only so that no-one can change the value of the input field. I’ve not seen this used much around but it was worth knowing.

HTML

Add read-only with jQuery

$('input').attr('readonly', true);

Remove read-only with jQuery

$(#input').removeAttr('readonly');
Don’t forget to put your jQuery code inside a document ready function. See 4 different jQuery document ready examples for more info.

Frequently Asked Questions (FAQs) about jQuery Read-Only Input Field

How can I make an input field read-only in jQuery?

To make an input field read-only in jQuery, you can use the attr() method. This method sets or returns attributes and values of the selected elements. Here’s an example of how to use it:

$(document).ready(function(){
$("input").attr("readonly", true);
});
In this example, the attr() method sets the readonly attribute to true for all input fields when the document is ready.

Can I toggle the read-only attribute in jQuery?

Yes, you can toggle the read-only attribute in jQuery. You can use the prop() method to achieve this. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
$("input").prop("readonly", function(i, val){
return !val;
});
});
});
In this example, clicking the button will toggle the readonly attribute of all input fields.

What is the difference between attr() and prop() methods in jQuery?

The attr() method gets the attribute value for only the first element in the matched set. It returns undefined for values of undefined elements. On the other hand, the prop() method gets the property value for only the first element in the matched set. It returns undefined for values of undefined elements. The attr() method sets or changes the specified attribute to the specified value, while the prop() method sets or changes the properties and values of the selected elements.

How can I remove the read-only attribute from an input field in jQuery?

You can remove the read-only attribute from an input field in jQuery using the removeAttr() method. Here’s an example:

$(document).ready(function(){
$("button").click(function(){
$("input").removeAttr("readonly");
});
});
In this example, clicking the button will remove the readonly attribute from all input fields.

Can I set the read-only attribute for specific input fields only?

Yes, you can set the read-only attribute for specific input fields only. You can do this by selecting the specific input field using its id or class. Here’s an example:

$(document).ready(function(){
$("#myInput").attr("readonly", true);
});
In this example, the attr() method sets the readonly attribute to true for the input field with id “myInput” when the document is ready.

Can I use the readonly attribute with other form elements?

The readonly attribute can be used with input and textarea elements. It does not work with select elements or option elements.

What is the difference between the readonly attribute and the disabled attribute?

The readonly attribute allows the user to focus on the input element and select its text, but they cannot change the value. On the other hand, the disabled attribute does not allow the user to interact with the input element at all.

Can I style read-only input fields differently?

Yes, you can style read-only input fields differently using CSS. You can use the :read-only pseudo-class to select read-only input fields. Here’s an example:

input:read-only {
background-color: #ddd;
}
In this example, read-only input fields will have a grey background color.

Can I make an input field read-only based on a condition?

Yes, you can make an input field read-only based on a condition. You can use an if statement to check the condition and then use the attr() method to set the readonly attribute. Here’s an example:

$(document).ready(function(){
if(condition){
$("input").attr("readonly", true);
}
});
In this example, if the condition is true, the attr() method sets the readonly attribute to true for all input fields.

Can I use the readonly attribute with input types other than text?

Yes, you can use the readonly attribute with various input types such as password, search, tel, url, email, and number. However, it does not work with input types such as radio, checkbox, file, range, color, and button.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week