Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu May 10, 2023 › #checkbox #HowTo

How to enable TextBox when checkbox is checked in JavaScript?

Enabling a textbox when a checkbox is checked in JavaScript can be done with just a few lines of code.

This can be useful for creating forms that require certain information to only be filled out if the checkbox is checked. It also allows for more dynamic user interaction on webpages and applications.

In order to enable the textbox when the check box is clicked, you need two elements: an HTML input element of type checkbox and an HTML input element of type text (or textarea).

We will refer to them as myCheckBox and myTextBox respectively in our example code below:

<input id="myCheckBox" type="checkbox">   
<input id="myTextBox" disabled> 

Next, we will add an event listener which listens for clicks on our myCheckBox element. When it detects one, it checks whether or not the check box has been selected by checking its checked property.

If so then it enables our myTextBox by setting its disabled property to false - allowing users to enter data into this field! The following JavaScript snippet shows how this could work:

save
listen
AI Answer
Write Your Answer
loading
back
View All