The HTML readonly attribute
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
<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
readonlyfields 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.























