Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
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 20-Jan-11 4:57am
laziale729
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
<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
  Permalink  
v2
Comments
laziale at 20-Jan-11 13:26pm
   
Thanks, that was the problem
Manfred R. Bihy at 20-Jan-11 13:33pm
   
Great that you could work it out!
Espen Harlinn at 25-Feb-11 7:44am
   
Right, My 5++
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Code is absolutely fine.
 
Hope you have added javascript at proper place, please refer this code snippet.
 
<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>
  Permalink  
Comments
laziale at 20-Jan-11 10:35am
   
I have as part of web user control.
E.F. Nijboer at 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)

  Print Answers RSS
0 OriginalGriff 274
1 Shweta N Mishra 216
2 PIEBALDconsult 210
3 Sergey Alexandrovich Kryukov 185
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100