Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello everybody I have JavaScript function that show and hide div. I need to call it on checkbox oncheckedchanged event but it always gives me an error here is the javascript function.

XML
<script language="javascript" type="text/javascript">
function showHideDiv(divID)
{

divstyle = document.getElementById("divID").style.visibility;
if(divstyle.toLowerCase()=="visible" || divstyle == "")
{
document.getElementById("divID").style.visibility = "hidden";

}
else
{
document.getElementById("divID").style.visibility = "visible";

}
}
</script>

and here is how I called it:
XML
<td class="style2">
                 <asp:CheckBox ID="YES" runat="server" AutoPostBack="True"
            Text="yes" oncheckedchanged="showHideDiv(divID);" />

             </td>
             <td class="style2">
               <asp:CheckBox ID="NO" runat="server"  AutoPostBack="True"
                    Text="no" oncheckedchanged="showHideDiv(divID);"/>
             </td>



and here is my div:
XML
<div id="Comments"   >
                  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                  </div>



and I want to make it hiddden by default

and there is another question I want to uncheck check box after the other is checked i need to make it using c#, now can i call the javascript function then execute c# code
any one can help ,
thanks in advance
Posted
Updated 12-Sep-10 4:48am
v3

OnCheckedChanged is a Server side event. So you are not allowed to give the javascript function here.

You need to register the javascript from the server or use

input type="CheckBox" and remove runat=server totally.

<asp:CheckBox OnCheckedChanged="checkchanged" runat="server" AutoPostBack=true/>

C#
protected void checkchanged(object sender, EventArgs e)
       {
           this.ClientScript.RegisterStartupScript(this.GetType(), "registerscript", "alert('hi');", true);
       }



Hence you can see the postback produces the alert.
 
Share this answer
 
Comments
eslam soliman 12-Sep-10 12:16pm    
yes it works too thanks a lot.
Abhishek Sur 12-Sep-10 12:23pm    
Most welcome.
Dalek Dave 13-Sep-10 7:08am    
Good Answer.
What error are you getting ?

You have to chage the below codeblock first.
XML
<asp:CheckBox ID="YES" runat="server" AutoPostBack="True"
           Text="yes" oncheckedchanged="showHideDiv(divID);" />
            </td>


You have to Pass your DIVID, Which is "Comments". It should be like
C++
oncheckedchanged="showHideDiv(Comments);"


Change the code with this and let me know if you have any other issue.

Thanks !
 
Share this answer
 
Comments
eslam soliman 12-Sep-10 11:20am    
Abhijit,
i try it but it gives me that error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1026: ) expected

Source Error:

Line 25:  Line 26: Line 27: <asp:checkbox id="YES" runat="server" autopostback="True"
line="" 28:="" text="yes" oncheckedchanged="showHideDiv(Comments);">
Line 29:
eslam soliman 12-Sep-10 11:21am    
it gives me the error in that line
<asp:CheckBox ID="YES" runat="server" AutoPostBack="True"
eslam soliman 12-Sep-10 11:29am    
i think that runat server property make some problems or becaue i put that code inside user control iam trying to solve the problem i know that it is simple but i cant find answer
thanks i solve the problem it was easy but i was not focus:
that is the code i use ,the error was not on oncheckedchanged ,i have to use on click
XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="java.WebUserControl1" %>
<style type="text/css">
    .style1
    {
        width: 100%;
    }
</style>
<script language="JavaScript" type="text/javascript">
    function toggle(id) {
        var state = document.getElementById(id).style.display;
        if (state == 'block') {
            document.getElementById(id).style.display = 'none';
        } else {
            document.getElementById(id).style.display = 'block';
        }
    }
    function hide(id) {
     var state = document.getElementById(id).style.display;
     if (state == 'block') {
         document.getElementById(id).style.display = 'none';
     }
    }
    </script>
<table class="style1">
    <tr>
        <td>
            &nbsp;</td>
        <td>
          <asp:checkbox id="no" OnClick="JavaScript:hide('Comments');" Runat="server" />
        </td>
        <td>
          <asp:checkbox id="yes" OnClick="JavaScript:toggle('Comments');" Runat="server" />
        </td>
    </tr>
    <tr>
        <td colspan="3">
                  <div id="Comments" style="display:none"  >
                  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                  </div>
         </td>
    </tr>
</table>
 
Share this answer
 
Comments
Abhijit Jana 12-Sep-10 12:01pm    
Great !
Hey Buddy..

you can use the OnChange property ...

eg:

<asp:checkbox id="no" OnChange="hide('Comments');" Runat="server" />
 
Share this answer
 

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