Set a form control's disabled property to true or false when another choice determines whether the control should be available. A common example is enabling an "Other" text box only when the user selects an "Other" checkbox.
const otherCheckbox = document.getElementById("others");
const otherText = document.getElementById("other_text");
otherText.disabled = !otherCheckbox.checked;
Try the enable/disable demo
otherText.disabled = true; // user cannot edit it
otherText.disabled = false; // user can edit it
The HTML disabled attribute can provide the initial state before JavaScript runs.
<label>
<input type="checkbox" id="others">
Other
</label>
<input type="text" id="other_text" disabled>
otherCheckbox.addEventListener("change", () => {
otherText.disabled = !otherCheckbox.checked;
if (!otherText.disabled) {
otherText.focus();
}
});
The older version used a body onload technique to disable the text box. A better default is to include the disabled attribute in the HTML and let JavaScript respond to later changes. This avoids a brief moment where the control is enabled before the script runs.
When several checkboxes each control their own text box, connect them with a data attribute instead of writing a separate function for every pair.
for (const checkbox of document.querySelectorAll("[data-controls]")) {
checkbox.addEventListener("change", () => {
const input = document.getElementById(checkbox.dataset.controls);
input.disabled = !checkbox.checked;
});
}
The second sitemap demo, multiple enable/disable controls, now demonstrates this pattern without relying on a missing server-side checker endpoint.
A disabled control cannot be interacted with and is normally excluded from form submission. A read-only text control can still receive focus and its value is submitted, but the user cannot edit it. Choose based on the behavior you need rather than using the two properties interchangeably.
The original related links are preserved: disable a checkbox, disable a listbox, and disable period/date controls.
JavaScript Checkbox Reference Disable CheckboxAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Sumith Harshan | 07-01-2011 |
| I found everywhere this one.finally i found this by in this site.very simple and useful.good job.thank you.good luck. | |
| Mike | 30-12-2011 |
| how to use drop down list instead checkbox in this? | |
| Chris | 03-06-2012 |
| This works as described, but what if I want to have multiple disabled text fields linked to multiple checkboxes? The result for me is that checking ONE box enables ALL the text fields, but I would like it to only enable the one. Any ideas? Thanks! | |
| newbie | 02-08-2012 |
| this was helpful for me! thank you bro.. | |
| Merline | 05-04-2013 |
| This Works.. Really superb.. | |
| Sait | 31-05-2014 |
| can you help me how to disable submit button over the due date?? | |
| smo | 31-05-2014 |
| You can read how to disable submit button till all the inputs are validated. You can add one more condition to it. But if you take JavaScript client date then that can be changed by user so you have to match with server side date. | |
| Angelica | 25-07-2014 |
| thanks it works perfect with textboxes but I also need to be able to enable/disable radio and dropboxes. Please could you tell me how to do this? I tried with your code and it didn't work :( | |
| Freddy | 18-11-2014 |
| it should put in body or anywhere can put it ? onload=enable_text(false);> i tested for secodn if it works i will coment again :D | |
| taomasaba | 28-11-2014 |
| You can choose 2 step step 1 set onload=enable_text(false); step 2 not set onload=enable_text(false); and set textbox to diasable Ex. <input type=text name=other_text diasabled> | |
| JALO CREER | 05-12-2014 |
| THANK FOR THIS ONE SIR | |
| Php Professional | 13-06-2017 |
| Great one. This tutorial was really excellent and useful to create a input box with enable and disable method on my new business site. Thanks. | |