Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable textbox depending on checkbox checked

Tags:

javascript

Can anyone please tell me how to disable a textbox, if a checkbox is checked, and enable textbox if the checkbox is not checked?

like image 213
Suresh Avatar asked Oct 07 '09 13:10

Suresh


People also ask

How do I disable a TextBox?

Disable the TextBox by adding the e-disabled to the input parent element and set disabled attribute to the input element.

How do you make a checkbox read only?

How do I make a checkbox readonly in JavaScript? A checkbox HTML element doesn't have a "readonly" property. Consequently, the only way to make a checkbox appear to be "readonly" is to disable the control.

How do I stop a checkbox from being unchecked?

Use the preventDefault() method to stop the default behavior of a particular element.


2 Answers

Put this in the checkbox:

onclick="document.getElementById('IdOfTheTextbox').disabled=this.checked;"
like image 153
Guffa Avatar answered Sep 16 '22 17:09

Guffa


    <input type="text" id="textBox">
    <input type="checkbox" id="checkBox" onclick="enableDisable(this.checked, 'textBox')">
    <script language="javascript">
    function enableDisable(bEnable, textBoxID)
    {
         document.getElementById(textBoxID).disabled = !bEnable
    }
</script>
like image 30
kemiller2002 Avatar answered Sep 17 '22 17:09

kemiller2002