views:

83

answers:

5

Greetings, I have asp.net check box control and asp.net dropdownlist control inside div tag.

I want to hid the div when I check the box and unhide it when I unchecked the box.

I tried few ways with jquery but I could not do it.

Here is my code please look at it and tell me what is wrong with it.

 <%@ Page Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="IMAM_APPLICATION.WebForm1" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

  <script src="js/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

    <script type="text/javascript">
        function ModifyOccup() {

            $('myOccup').removeClass('display1');
            $('myOccup').removeClass('display1');
            $('myOccup').removeClass('display');
            $('myOccup').removeClass('display');





            if ($('#<%=chkOccup.ClientID %>').is(':checked')) {

                $('myOccup').addClass('display1');
            }
            else {
                $('myOccup').addClass('display');
            }
        }

                </script>


                  <asp:CheckBox ID="chkOccup" runat="server" Style="top: 1055px; left: 355px;
                    position: absolute; height: 22px; width: 126px" Text="Check to modify" onclick=" ModifyOccup()"/>


         <div id ="myOccup" >

                 <asp:DropDownList ID="cmbWorkField" runat="server" Style="top: 1090px; left: 350px;
                    position: absolute; height: 22px; width: 126px">

                </asp:DropDownList>


      </div>
</asp:Content>



......................
Style.css File
..........................


    .display { display: none; }
    .display1 { display: inherit; }
A: 

Give this a try...

$('#<%=chkOccup.ClientID %>').click(function(e)
{
    this.checked ? $('#myOccup').show() : $('#myOccup').hide();
}
Wallace Breza
I would bind to 'change' instead as not everyone uses the mouse to trigger checkboxes.
Agent_9191
Thank you for your try...I tried your way but did not work!
Eyla
Sorry, had a syntax issue... It should work now.
Wallace Breza
+3  A: 

When you select things with jQuery you need to use CSS Selector syntax, so $('#myOccup') instead of what you have there.

Alex Sexton
He's attempting to get the client id of the element using <%=chkOccup.ClientID %> so when the page renders it will be equal to $('#myOccup').
Wallace Breza
Alex is correct, you may use this $('#myOccup') or $('[id$=myOccup]')
Rbacarin
Oops, thought you were referring to the way he was getting the checkbox, not the div.
Wallace Breza
+2  A: 

with this: $('[id$=myOccup]') you'll get only the part that really matter to you in the id, even if your control is runat=server;

Rbacarin
@Rbacarin - +1 - Never saw that before. Is that an extension that jQuery put in for attribute selectors, or is that a standard CSS attribute selector?
Wallace Breza
Friend Wallace,I've started to use this, after I read this documentation:http://api.jquery.com/attribute-ends-with-selector/instead of use ...name$=... I use ...id$=...
Rbacarin
@Rbacarin - That's great. Thanks for the info. I'll definitely use this.
Wallace Breza
+1  A: 

This is similar to Wallace Breza's answer with some fixes.

 $(function(){

         $('#<%=chk0ccup.ClientID %>').change(function(e)
        {

            this.checked ? $('#myOccup').show() : $('#myOccup').hide();
        });
   });

With this script you should remove everything you have in the <script> tags, your CSS, and the onclick function of the checkbox.

orandov
A: 

Here is a slide up/down option that should work.

$('#chkOccup').click(function(){
  $('#myOccup').slideToggle();           
});

I also think you were close.. you forgot to enter the # in the selector:

$('myOccup').addClass('display1');

Should be

$('#myOccup').addClass('display1');
croteau