CKTIS / JavaScript
`value` and `disabled` on input and textarea elements
Read and update a form control’s value independently of whether the user can interact with it. Distinguish an element’s own `disabled` attribute from its effective disabled state and see which values make it into form data.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
control.value // read a string
control.value = text // write a string; input.type determines the rules
control.disabled // read the element's own disabled attribute as a boolean
control.disabled = true
control.disabled = false
control.matches(':disabled') // test whether the control is actually disabledThe catalog label HTMLInputElement/HTMLTextAreaElement.value/disabled refers to two properties on each of two HTML DOM interfaces. They are web platform APIs, not features of ECMAScript 2024 itself. For text inputs and textareas, value reads and writes the current string. A textarea normalizes line endings in value to LF (\n). The default (defaultValue) comes from the input’s value attribute or the textarea’s text content. After a user edit or a value assignment, changing the default no longer overwrites the current value. A form reset sets the current value from the default, but an input also runs its type’s value sanitization algorithm, if one is defined. For example, a type="text" input strips line breaks from value on reset even if they are present in defaultValue.
For inputs, type determines how value works. In value mode, as with text and number, the browser may transform or clear the assigned string according to the type’s rules; number still returns a string. For hidden, value reflects the value attribute and returns '' if absent. For checkbox and radio, it also reflects the attribute but falls back to 'on'; use checked for selection state. For file, reading value does not reveal the real path: use files instead. Assigning '' clears the selection; assigning a nonempty string throws InvalidStateError.
The Boolean property disabled reflects only the element’s own attribute: true adds it and false removes it. In HTML, even disabled="false" disables the control because presence is what counts. A control can have disabled === false and still be disabled by an ancestor fieldset, except when it is inside that fieldset’s first legend child. Use control.matches(':disabled') to test the effective state.
A disabled control cannot receive focus or be edited by the user, is excluded from constraint validation, and is omitted from form submission and new FormData(form). Scripts can still read and write value; assignment alone does not fire input or change. Where supported, use readOnly if users should still be able to focus the field but not edit it; readOnly does not by itself exclude the field from submission. Neither state replaces server-side validation or authorization.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Form control value and disabled state</title>
</head>
<body>
<script>
(() => {
const form = document.createElement('form');
const group = document.createElement('fieldset');
const input = document.createElement('input');
const textarea = document.createElement('textarea');
input.type = 'text';
input.name = 'title';
textarea.name = 'note';
group.append(input);
form.append(group, textarea);
group.disabled = true;
input.value = 'Draft';
textarea.value = 'Line 1\r\nLine 2';
console.log(JSON.stringify([input.value, input.disabled, input.matches(':disabled')]));
console.log(JSON.stringify([...new FormData(form)]));
group.disabled = false;
textarea.disabled = true;
console.log(JSON.stringify([...new FormData(form)]));
})();
</script>
</body>
</html>Result
["Draft",false,true] [["note","Line 1\nLine 2"]] [["title","Draft"]]
Save the example as an HTML file, open it in a browser, and check the browser console. The first line shows that `Draft` can be assigned even though the input is effectively disabled and its own `disabled` is `false`. The next two lines show form data before and after switching the controls’ states; JSON displays the LF line ending as `\n`.
Related lessons
- JavaScript · Lesson 4 · Advanced · Version: 4
Sources
- HTML Living Standard — The input element
- HTML Living Standard — The textarea element
- HTML Living Standard — Enabling and disabling form controls: the disabled attribute
- HTML Living Standard — Constructing the entry list
- HTML Living Standard — Common event behaviors
- MDN — FormData: FormData() constructor
- MDN — disabled HTML attribute