Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

readonly — a read-only form control

The readonly boolean attribute prevents user editing in supported input controls and textarea, but does not prevent JavaScript changes or itself exclude values from form submission. For form-associated custom elements, its built-in behavior is narrower.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<input type="text" name="code" value="A17" readonly>
<textarea name="note" readonly>Fixed text</textarea>
// JavaScript: element.readOnly = true; element.readOnly = false;

readonly is a boolean attribute: its presence enables read-only mode. Valid forms include readonly, readonly="", and readonly="readonly". Writing readonly="false" is not a valid way to turn it off—it still means the attribute is present. Remove the attribute or set the JavaScript property readOnly to false; note the uppercase O.

The attribute applies to textarea and these input types: text, search, tel, url, email, password, date, month, week, time, datetime-local, and number. It does not apply to other types, such as checkbox, radio, range, file, or buttons, or to select. On supported controls, it prevents the user from changing the value, but scripts can still assign a new value through value.

Unlike disabled, readonly alone does not disable the control: it remains focusable, and its name and value can be included in form data under the usual rules. A read-only control is barred from constraint validation (willValidate is false); required must not appear together with readonly. Do not treat this attribute as a data-integrity security measure—client-submitted data requires independent server-side validation.

For a form-associated custom element, the presence of readonly also bars constraint validation. However, the browser does not automatically prevent editing: the component author should implement that behavior. The example demonstrates a script changing value, read-only controls being included in FormData, and the disabled control being omitted.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>readonly demo</title>
<form id="demo">
  <label>Code <input name="code" value="A17" readonly></label>
  <label>Note <textarea name="note" readonly>Fixed text</textarea></label>
  <label>Excluded <input name="excluded" value="X" disabled></label>
</form>
<pre id="result"></pre>
<script>
  const form = document.getElementById('demo');
  const code = form.elements.namedItem('code');
  const lines = [
    `readOnly: ${code.readOnly}`,
    `willValidate: ${code.willValidate}`
  ];
  code.value = 'B23';
  lines.push(`data: ${JSON.stringify([...new FormData(form)])}`);
  code.readOnly = false;
  lines.push(`readonly present: ${code.hasAttribute('readonly')}`);
  document.getElementById('result').textContent = lines.join('\n');
</script>
</html>

Result

readOnly: true
willValidate: false
data: [["code","B23"],["note","Fixed text"]]
readonly present: false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources