Explain Codes LogoExplain Codes Logo

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

javascript
dom-manipulation
javascript-best-practices
jquery
Nikita BarsukovbyNikita Barsukov·Jan 15, 2025
TLDR

To make an <input> uneditable, apply the readonly attribute.

HTML:

<input type="text" readonly>

JavaScript:

document.querySelector('input').readOnly = true;

This action locks the field, leaving its value ready for submission but unmodifiable by users.

Dynamic attributes: Mastering the toggle

Depending on your application's requirements, you may need to turn on/off the readonly state of an <input>.

Native JavaScript:

// Read-only mode activated. Party's over, folks! document.querySelector('#targetInput').readOnly = true; // Now it's editable. Let the keys tap again! document.querySelector('#targetInput').readOnly = false;

Using jQuery:

// For > jQuery 1.9. DJ ReadOnly is in the house! $('#targetInput').prop('readonly', true); // DJ ReadOnly has left the building. Party on! $('#targetInput').prop('readonly', false); // For < jQuery 1.9: $('#targetInput').attr('readonly', 'readonly'); $('#targetInput').removeAttr('readonly');

Why .prop and not .attr? The prop() method gets the property value for only the first element in the matched set. It returns undefined for values of undefined properties, while attr() gets the attribute value for only the first element in the matched set.

Error-free ride: A guide to avoiding blunders

Even with this knowledge, errors can creep into our code. Here are some tips to keep them at bay:

  • Confirm your selectors are correct. Use the ID selector with a hash (#targetInput) when working with IDs.
  • Remember that readonly ensures that the input value is still submitted with the form, unlike a disabled field.
  • Always mind your syntax. For instance, the correct syntax is .prop('readonly', true), not .prop('readOnly', true).

Noteworthy specifics about the "readonly" field

Certain subtleties and quirks come with read-only fields. Be aware:

Form submission behavior: Values from readonly fields are indeed submitted with the form, unlike fields with the disabled attribute.

CSS Styling:

input[readonly] { background-color: #e9ecef; color: #495057; }

Because presentation is key, right?

Accessibility: Ensure assistive technologies can make sense of readonly fields. Just doing our inclusive, accessible code part!

Security note: Do not depend on readonly to enforce security restrictions on sensitive fields. Because client-side controls are like a bicycle lock in a city that never sleeps!

The only constant is "change": jQuery vs Vanilla JS

Sometimes, you'll face a dilemma: When to use jQuery over plain JavaScript?

  • If you’re deeply immersed in jQuery in your project, stay the jQuery course for DOM manipulations.
  • However, when your tasks are simple or in projects where every millisecond is precious, native JavaScript is often faster and more efficient.

As the wise say, consistency is key. Avoid mixing different approaches unless you're in a mood to spin your future-self in a debugging whirl!