How to Disable an Input with the disabled Property in JavaScript
Set element.disabled = true and a text box, button or other form control turns grey and stops responding: nobody can type in it, click it or select it. Set it to false and it works again.
Syntax
<control>.disabled = true;
<control>.disabled = false;
| Part | What it does |
|---|---|
<control> | A form control: an <input>, <button>, <textarea> or <select>, for example from document.getElementById. |
true | Disables it. The browser greys it out and ignores clicks and typing. |
false | Enables it again. |
You can also set it from a comparison, seat.disabled = seat.value === 'B3', because a comparison is true or false. MDN's HTMLInputElement: disabled property page describes the property for inputs; buttons, text areas and lists have the same one.
The Example
The example page has two rows, each with a text box and a button. The script disables the second row's text box and button, so you can compare the two rows.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>disabled</title>
</head>
<body>
<p><input id="name" value="Ana"> <button type="button" id="save">Save</button></p>
<p><input id="code" value="ABC-123"> <button type="button" id="delete">Delete</button></p>
<script>
document.getElementById('code').disabled = true;
document.getElementById('delete').disabled = true;
</script>
</body>
</html>
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

Try to click into the grey text box and type: nothing happens. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The second text box and button are grey, the browser's look for disabled controls.
- Clicking the disabled text box didn't select it. After a click on it and a key press, the focus was still on the page itself, and the value was still
ABC-123. - The first row works as usual, because only the second row's controls were disabled.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Setting it to the string 'false' | The control stays disabled: disabled became true, because any non-empty string counts as true | Use true and false without quotes |
| Disabling a field whose value the form must send | A disabled field isn't sent: a form with an enabled a and a disabled b sent only ?a=one | Use readOnly = true when people mustn't change a value that still has to be sent |
| Expecting a click listener to run on a disabled button | A click on a disabled button didn't run its listener | Enable it first, or show why it's disabled |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A field's value is missing from the form data | The field is disabled | Use readOnly, or enable it before the form is sent |
| A button stays grey | disabled is still true, or was set to a string | Set disabled = false |
Cannot set properties of null (setting 'disabled') | The control wasn't found | Check the id, and run the script after the control |
FAQ
What's the difference between disabled and readOnly?
A read-only text box can still be selected and its text copied, and its value is sent with the form. A disabled one can't be selected, and it isn't sent. readOnly works on text boxes and text areas only.
Can I disable it in the HTML instead?
Yes, with the disabled attribute: <input disabled>. The property and the attribute are the same setting, so a script can turn it off with disabled = false.
Can I disable a whole group of controls at once?
Put them in a <fieldset> and disable that: fieldset.disabled = true disables every control inside it.
Conclusion
disabled = true greys out a control and stops people using it, and disabled = false brings it back. Remember that a form doesn't send disabled fields: use readOnly when the value still has to go.
Additional Resources
- HTMLInputElement: disabled property, on MDN.
- HTML attribute: disabled, on MDN.
- HTML attribute: readonly, on MDN.
