2
<script type="text/javascript">
    $(document).ready(function() {
        $('#pHeader').click(function() {
            $('#pBody').slideToggle('fast');
        });
    });
 </script>

<asp:Panel ID="pHeader" runat="server" CssClass="cpHeader">
    <asp:Label ID="lblText" runat="server" Text="Header" />
</asp:Panel>

<asp:Panel ID="pBody" runat="server" CssClass="cpBody">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna
    aliqua. Ut enim ad minim veniam, quis nostrud exercitation
    ullamco laboris nisi ut aliquip ex ea commodo consequat.
    Duis aute irure dolor in reprehenderit in voluptate velit
    esse cillum dolore eu fugiat nulla pariatur
</asp:Panel>

As you can see from the above I am trying to achieve a collapsible panel. However the above will only work once. How can I make this accessible to multiple controls, i.e. if I have 10 panels on a page?

Thanks guys!

3 Answers 3

1

Try this:

$(function(){
  $('.cpHeader').click(function() {
    $(this).next('.cpBody').slideToggle('fast');
  });
});

If you select by ID (e.g., #pHeader), you'll only affect one node, since IDs are meant to be unique. Classes are meant to be assigned to multiple nodes.

Also note the shortened first line, which jQuery provides as a shortcut for $(document).ready().

Sign up to request clarification or add additional context in comments.

2 Comments

If select by class for the cpBody though you will toggle all body panels whenever any of the headers are clicked so they will not work independantly.
Actually, my original was a bit incorrect in that it was looking for a .cpBody nested inside the clicked .cpHeader, which the question's code didn't contain. I've updated my code so that it uses .next() to get the .cpHeader's next sibling, rather than .find(), which selects children.
1

I guess you have to use $('.pHeader') and $('.pBody') instead of $('#pHeader') and $('#pBody') if you assign the CSS class pHeader to every Panel.

The "#" character selects an element with the id after the "#" sign. The "." is used to get all elements to which you apply a certain CSS class name.

So, code this should work:

<script type="text/javascript">
$(document).ready(function() {
     $('.pHeader').click(function() {
         $('.pBody').slideToggle('fast');
     });
});
</script>

Comments

0

Hi the main problem you have with ASP.NET is that the controls will create a custom client side ID for each element when within a custom control. To avoid this trap, place the code you have inside an ascx custom control like this, putting serverside inline code to write out the custom client id. this way you can have multiple controls all working independently.

<asp:Panel ID="pHeader" runat="server" CssClass="cpHeader">
    <asp:Label ID="lblText" runat="server" Text="Header" />
</asp:Panel>

<asp:Panel ID="pBody" runat="server" CssClass="cpBody">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna
    aliqua. Ut enim ad minim veniam, quis nostrud exercitation
    ullamco laboris nisi ut aliquip ex ea commodo consequat.
    Duis aute irure dolor in reprehenderit in voluptate velit
    esse cillum dolore eu fugiat nulla pariatur
</asp:Panel>



 <script type="text/javascript">
        $(function() {
            $('#<%=this.pHeader.ClientID %>').click(function() {
                $('#<%=this.pBody.ClientID %>').slideToggle('fast');
            });
        });
     </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.