Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
XML
0 down vote favorite

HEllo - I am trying to enable/disable textbox when checkbox is checked (enable) or unchecked (disable). WIth the piece of code i have nothing is happening once the checkbox is checked/unchecked. Here is what I have:
<script type="text/javascript" language="javascript">
    function enableTextBox() {
        if (document.getElementById("chkAssociation").checked == true)
            document.getElementById("txtAddress").disabled = false;
        else
            document.getElementById("txtAddress").disabled = true;
    }
</script>
and than the control:
<asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation"  runat="server" />&nbsp;&nbsp;
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />



Thanks for helping
Posted

I spotted you problem. The ID attribute in the asp:checkbox tag is not the same as the ID attribute of the client side (HTML) tag. I'll try to rectify your code, but must say that since I can't try this out it might need some tweaking on your side:

XML
<script type="text/javascript" language="javascript">
    function enableTextBox() {
        var textBoxID = "<%= txtAddress.ClientID %>";
        if (document.getElementById("<%= chkAssociation.ClientID %>").checked == true)
            document.getElementById(textBoxID).disabled = false;
        else
            document.getElementById(textBoxID).disabled = true;
    }
</script>
and than the control:
<asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation"  runat="server" />
<asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />


You'll have to look it up really how the client ID is fetched from the control on the server side. It could be a property like I indicated, but might as well be a method. Main point is though that the ID attribute in the server side asp:textbox and asp:checkbox tags is NOT the same as the ID attribute that gets output into the rendered HTML.

Hope that helps you!

Best Regards,
Manfred
 
Share this answer
 
v2
Comments
laziale 20-Jan-11 13:26pm    
Thanks, that was the problem
Manfred Rudolf Bihy 20-Jan-11 13:33pm    
Great that you could work it out!
Espen Harlinn 25-Feb-11 7:44am    
Right, My 5++
Code is absolutely fine.

Hope you have added javascript at proper place, please refer this code snippet.

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function enableTextBox()
    {
        if (document.getElementById("chkAssociation").checked == true)
            document.getElementById("txtAddress").disabled = false;
        else
            document.getElementById("txtAddress").disabled = true;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox Checked="false" OnChange="javascript:enableTextBox();" ID="chkAssociation"  runat="server" />
        <asp:TextBox ID="txtAddress" Enabled="false" Text="Test" runat="server" />
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
laziale 20-Jan-11 10:35am    
I have as part of web user control.
E.F. Nijboer 20-Jan-11 10:46am    
Or somewhat shorter:
function enableTextBox()
{
   document.getElementById("txtAddress").disabled = !document.getElementById("chkAssociation").checked;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900