Skip to main content

How to Disable an Input with the disabled Property in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<control>.disabled = true;
<control>.disabled = false;
PartWhat it does
<control>A form control: an <input>, <button>, <textarea> or <select>, for example from document.getElementById.
trueDisables it. The browser greys it out and ignores clicks and typing.
falseEnables 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:

index.html, a page with two paragraphs: a text box with Ana and a Save button, and a text box with ABC-123 and a Delete button, and a script that sets disabled to true on the code text box and the Delete button.

index.html
<!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:

The page in Chromium: a normal text box with Ana and a Save button, and below them a greyed-out text box with ABC-123 and a greyed-out Delete button.

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​

MistakeWhat happensDo this instead
Setting it to the string 'false'The control stays disabled: disabled became true, because any non-empty string counts as trueUse true and false without quotes
Disabling a field whose value the form must sendA disabled field isn't sent: a form with an enabled a and a disabled b sent only ?a=oneUse readOnly = true when people mustn't change a value that still has to be sent
Expecting a click listener to run on a disabled buttonA click on a disabled button didn't run its listenerEnable it first, or show why it's disabled

Troubleshooting​

SymptomLikely causeFix
A field's value is missing from the form dataThe field is disabledUse readOnly, or enable it before the form is sent
A button stays greydisabled is still true, or was set to a stringSet disabled = false
Cannot set properties of null (setting 'disabled')The control wasn't foundCheck 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​