References

Beginner-friendly references for web development, with live, editable examples.

The HTML readonly attribute

Attribute All modern browsers
In short

The HTML readonly attribute stops users from changing the value of a text field or textarea. Unlike disabled, a readonly field still takes focus and is sent with the form. Checkboxes, radio buttons and select menus aren't locked by it.

Overview

The HTML readonly attribute locks the value of a text field or <textarea> against edits by the user. The field still takes focus, and its name and value go out with the form like those of any other field.

A disabled field goes further. It can't be focused, it's left out of the submitted data, and it stops working as a control. Readonly suits a value that belongs in the submission but shouldn't change, such as an order number on a review page.

Only text-like controls can be read-only. That means text, search, url, tel, email, password and number inputs, the date and time types, and textarea. Other controls, such as checkboxes and buttons, are left out, because read-only and disabled have no useful difference on them.

A readonly field is barred from constraint validation. Its willValidate property is false, it matches neither :valid nor :invalid, and it never blocks a submission, even when its value breaks a pattern or falls below min. Its validity object can still report those mismatches.

Syntax

<label for="order">Order number</label>
<input id="order" name="order" value="A-1042" readonly>

Values

Value
Written alone or as readonly=readonly, it locks the value against edits.

Example

Live example
<form id="review">
  <p style="margin:8px 0"><label for="order">Order (readonly)</label>
  <input id="order" name="order" value="A-1042" readonly style="background:#eee;border:1px solid #999"></p>
  <p style="margin:8px 0"><label for="coupon">Coupon (disabled)</label>
  <input id="coupon" name="coupon" value="FALL10" disabled></p>
  <p style="margin:8px 0"><label for="note">Delivery note</label>
  <input id="note" name="note" value="Ring twice"></p>
  <button>Send</button>
  <p id="out" role="status" style="overflow-wrap:anywhere"></p>
  <p>Try to edit each field, then press Send.</p>
</form>
<script>
document.getElementById('review').addEventListener('submit', function (e) {
  e.preventDefault();
  const sent = [...new FormData(this)].map(pair => pair.join('=')).join(', ');
  document.getElementById('out').textContent = 'Sent ' + sent + '. The coupon was left out.';
});
</script>

Best practices

  • Make readonly fields look locked, for example with a muted background. In Chrome, a readonly input has the same background, border and text cursor as an editable one.
  • Skip readonly on checkboxes, radio buttons and file inputs, which it can't lock. Chrome and Firefox still switch off the required check on them, so an unticked or empty one goes through.
  • On the server, handle a readonly value like any other user input. A script can rewrite it, and the browser runs no constraint checks on it before sending.
  • Remove readonly before calling showPicker() on a date or time field. On a control that isn't mutable, such as a readonly one, showPicker() throws an InvalidStateError.

Accessibility

A readonly field stays in the Tab order, unlike a disabled one. Keyboard users can still reach it and read its value. If both readonly and aria-readonly are set, HTML-AAM tells browsers to expose only the readonly attribute's state.

Frequently asked questions

What is the difference between readonly and disabled?
A readonly field stays focusable and is submitted, while a disabled field is neither. Use readonly for a value that should be sent, and disabled for one that shouldn't.
Why can a readonly checkbox or select still be changed?
Text controls are the only ones readonly applies to, so checkboxes, radio buttons and select menus stay changeable. To lock one, use disabled, plus a hidden input when its value must still be submitted.
Can JavaScript change a readonly field?
Yes, readonly only blocks the user, so a script can still set the field's value. Toggle the attribute itself with the readOnly property, written with a capital O.
Does :read-only match only readonly inputs?
No, :read-only matches more than readonly fields, including checkboxes, buttons, disabled fields and ordinary page text. Use the [readonly] attribute selector to reach only fields with the attribute.
Is a readonly field checked by required or pattern?
No, a readonly field is barred from constraint validation, so required and pattern never stop its form. An empty readonly field with required still submits.
Is readonly=readonly the same as readonly?
Yes, the two forms mean the same thing, because only the presence of readonly counts. A bare readonly, an empty value and readonly=readonly are all valid, while true and false are not.