jQuery: Set and Remove disabled and readonly on Form Fields

Use .prop('disabled', true) to disable a form control in jQuery and false to enable it again. Use .prop('readOnly', true) for text inputs and textareas that should remain read-only. Checkboxes and selects do not support readonly.

How disabled and readonly differ

Control Supports disabled Supports readonly
input[type="text"] Yes Yes
textarea Yes Yes
input[type="checkbox"] Yes No
select Yes No

Disabled fields cannot be edited and are normally skipped by keyboard focus and form submission. Read-only text fields block editing but can still receive focus and allow text selection. They remain in the form data when enabled and named.

HTML standard for readonly

Set and clear the state with .prop()

Scope the selector to the relevant form. The code disables and then enables the controls, then sets and clears read-only on the text fields. The HTML attribute is readonly, and the DOM property is readOnly. In HTML, disabled="false" still disables the control because the attribute is present. Pass the boolean false to clear the property.

const $fields = $('#order :input[name]');
const $text = $('#order input[type="text"], #order textarea');

$fields.prop('disabled', true);
$fields.prop('disabled', false);

$text.prop('readOnly', true);
$text.prop('readOnly', false);

Official jQuery .prop() documentation

Complete example with form data

Save the code as form-states.html in UTF-8 and open it in a browser. The jQuery 4.0.0 build loads over the internet. Editable mode shows four entries. Select readonly and the title and notes cannot be edited, but all four are still listed. Select disabled and no entries remain. Return to Editable to restore access and see the values again. An unchecked checkbox is omitted even when enabled.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare form control states</title>
</head>
<body>
  <label for="mode">State</label>
  <select id="mode">
    <option value="edit">Editable</option>
    <option value="readonly">readonly</option>
    <option value="disabled">disabled</option>
  </select>
  <form id="order">
    <p><label>Title <input type="text" name="title" value="Alpha"></label></p>
    <p><label>Notes <textarea name="memo">Beta</textarea></label></p>
    <p><label>Plan
      <select name="plan">
        <option value="basic">Basic</option>
        <option value="plus">Plus</option>
      </select>
    </label></p>
    <p><label><input type="checkbox" name="news" value="yes" checked> Receive updates</label></p>
    <button id="inspect" type="button">Inspect form data</button>
  </form>
  <pre id="output" aria-live="polite"></pre>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
  <script>
    const form = document.querySelector('#order');
    const $fields = $('#order :input[name]');
    const $text = $('#order input[type="text"], #order textarea');

    function showData() {
      const items = Array.from(new FormData(form));
      const lines = items.map(([name, value]) => name + '=' + value);
      $('#output').text(lines.join('\n') || "No values included in form data.");
    }

    function setState() {
      const mode = $('#mode').val();
      $fields.prop('disabled', mode === 'disabled');
      $text.prop('readOnly', mode === 'readonly');
      showData();
    }

    $('#mode').on('change', setState);
    $('#inspect').on('click', showData);
    setState();
  </script>
</body>
</html>

HTML standard for building form data

Why an enabled control may still be disabled

.prop('disabled') reads the first matched element’s own property. A disabled parent fieldset can still disable a control whose property is false. Check the effective state with .is(':disabled') and inspect the fieldset. If a value is missing from form data, also check its name. Validate inputs on the server independently of the page’s controls.