Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLInputElement.max and disabled: value limits and disabled inputs

`max` reflects the upper limit as a string, while `disabled` reflects whether the input has its own disabling attribute. Exceeding the limit does not disable the input.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

input.max;                 // string: the max attribute, or "" if absent
input.max = "10";          // set the max attribute
input.removeAttribute("max");
input.disabled;            // boolean: whether the input's own disabled attribute is present
input.disabled = true;     // add the disabled attribute
input.disabled = false;    // remove the disabled attribute
input.matches(":disabled"); // test whether the input is effectively disabled

HTMLInputElement.max/disabled refers to two separate properties. The writable string property input.max reflects the HTML max attribute. It returns the attribute text, or "" if absent, not the computed upper limit. Set it with input.max = "10" and remove it with input.removeAttribute("max").

max applies to number, range, date, month, week, time, and datetime-local, with a format defined by the type. It does not limit text length. An absent or unparseable attribute falls back to the type's default maximum, if any: range defaults to 100, but number has no default maximum. A number above the limit stays in a number input's value and makes validity.rangeOverflow === true. A range slider instead adjusts its value to fit the range. A time range with min greater than max can span midnight.

The writable boolean input.disabled reflects the presence of the input's own disabled attribute: true adds it and false removes it. The HTML spelling disabled="false" still disables the input. A disabled fieldset also disables descendant inputs except those inside its first legend child. So input.disabled can be false even when input.matches(":disabled") is true.

Users cannot focus or edit a disabled input, but JavaScript can still change its value. A disabled input is excluded from constraint validation (willValidate is false) and omitted from form submission and new FormData(form). These are browser DOM properties, not ECMAScript features; a runtime without the DOM does not provide them. Always validate received data on the server regardless of the input settings.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Input max and disabled</title>
</head>
<body>
  <script>
    const form = document.createElement('form');
    const input = document.createElement('input');
    input.type = 'number';
    input.name = 'quantity';
    input.max = '10';
    input.value = '12';
    form.append(input);

    console.log(JSON.stringify([
      input.max, input.value, input.validity.rangeOverflow,
      input.checkValidity(), new FormData(form).has('quantity'),
    ]));

    input.disabled = true;
    console.log(JSON.stringify([
      input.disabled, input.value, input.willValidate,
      new FormData(form).has('quantity'),
    ]));
  </script>
</body>
</html>

Result

["10","12",true,false,true]
[true,"12",false,false]

Save the example as an `.html` file, open it in a browser, and check the output in the developer console. The value `"12"` exceeds `"10"` and fails validation, but `FormData` still includes it. After disabling, the input keeps its value but is excluded from validation and form data.

Related lessons

  • JavaScript · Lesson 28 · Intermediate · Version: 3

Sources