11

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

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Suresh
  • 38,717
  • 16
  • 62
  • 66

7 Answers7

51

Put this in the checkbox:

onclick="document.getElementById('IdOfTheTextbox').disabled=this.checked;"
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
18
    <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>
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
5
jQuery(document).ready(function () {
   $("#checkBox").click(function () {
      $('#textBox').attr("disabled", $(this).is(":checked"));
   });
});
Conner
  • 30,144
  • 8
  • 52
  • 73
Nooglesoft
  • 51
  • 1
  • 2
2

Create a Javascript function like this:

function EnableTextbox(ObjChkId,ObjTxtId)
{

    if(document.getElementById(ObjChkId).checked)
        document.getElementById(ObjTxtId).disabled = false;
    else
        document.getElementById(ObjTxtId).disabled = true;
}

Create a C# function like this on the grid RowDataBound:

protected void lstGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txtAllowed = (TextBox)e.Row.FindControl("txtAllowed");

        CheckBox chkAllowed = (CheckBox)e.Row.FindControl("RowSelector");
        chkAllowed.Attributes.Add("onClick", "EnableTextbox('" + chkAllowed.ClientID + "', '" + txtAllowed.ClientID + "')");
    }
}
sjngm
  • 12,423
  • 14
  • 84
  • 114
Kaju
  • 21
  • 1
1

I have the simplest solution yet for this Simple task. Believe me or not it works

s = 1;
   function check(){
       o = document.getElementById('opt');
       if(o.value=='Y'){
           s++;
           if(s%2==0)
           $('#txt').prop('disabled',true);
           else
           $('#txt').prop('disabled',false);
       }
       
   }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Text: <input type="text" name="txt" id="txt">
<input type="checkbox" name="opt" id="opt" value="Y" onclick="check()">

Here is code.

0
<script type="text/javascript">
    function EnableDisableTextBox(chkPassport) {
        var txtPassportNumber = document.getElementById("txtPassportNumber");
       txtPassportNumber.disabled = chkPassport.checked ? false : true;
        if (!txtPassportNumber.disabled) {
            txtPassportNumber.focus();
        }
    }
</script>
<label for="chkPassport">
<input type="checkbox" id="chkPassport" onclick="EnableDisableTextBox(this)" />
Do you have Passport?
</label>
<br />
Passport Number:
<input type="text" id="txtPassportNumber" disabled="disabled" />
dev dev
  • 121
  • 1
  • 2
  • 17
-2

Using jQuery:

$("#checkbox").click(function(){
  $("#textbox")[0].disabled = $(this).is(":checked");
});
Marius
  • 57,995
  • 32
  • 132
  • 151
  • I seriously can't believe that this answer survived for three years while not working at all... Either use `$("#textbox")[0].disabled` on the left side or replace the whole line with `$("#textbox").prop('disabled', ...);` - oh, and why not use `this.checked` on the right side? – ThiefMaster Sep 11 '12 at 22:28