views:

211

answers:

2

I am using the toggle() with jQuery and I have a sidebar on my page and each header is

<h2 class="sidebar-header">

One section of my code will look like:

 <div class="sidebar-section"> 
  <h2 class="sidebar-header">SUBSCRIBE</h2>
  <p class="sidebar">Make sure you subscribe to stay in touch with the latest articles and tutorials. Click on one of the images below:</p>
  <div class="rss-icons">
   <a href="http://fusion.google.com/add?feedurl=http://feeds.feedburner.com/ryancoughlin"&gt;&lt;img src="http://gmodules.com/ig/images/plus_google.gif" width="62" height="17" alt="Add to Google Reader or Homepage" class="feed-image"/></a> 
   <a href="http://add.my.yahoo.com/rss?url=http://feeds.feedburner.com/ryancoughlin" title="Web/Graphic Design/Development by Ryan Coughlin"><img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo3.gif" alt="Add feed to My Yahoo" width="62" height="17" class="feed-image" /></a>
   <a href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http://feeds.feedburner.com/ryancoughlin" title="Web/Graphic Design/Development by Ryan Coughlin"><img src="http://www.newsgator.com/images/ngsub1.gif" alt="Subscribe in NewsGator Online" class="feed-image" height="17" /></a>
  </div>
  <hr />
  <p class="sidebar feed">Don't have those? Grab the <a href="http://feeds.feedburner.com/ryancoughlin" target="_blank" title="Subscribe to this feed">RSS</a> url.</p>
  <p class="sidebar delicious">Make sure you add me to <a href="http://del.icio.us/post?url=http://www.ryancoughlin.com&amp;amp;title=Ryan Coughlin - Web/Graphic Design and Development" target="_blank" title="Add to delicious!">delicious!</a> </p>
 </div>

They are each wrapped in those DIV elements. I am trying to make it for if you click the header, it will shrink up the content. I know toggle can do that, but if I make it for each "sidebar-header", you will click any one element on the page and it will hide them all, how can I do this?

The next selector with jquery?

Thanks,

Ryan

+1  A: 

Try something like this:

Suppose you have the following code:

<div class="topdiv">
    <h2 class="header">Header 1</h2>
    <div class="somecontent">
        This is the content
    </div>
</div>

<div class="topdiv">
    <h2 class="header">Header 2</h2>
    <div class="somecontent">
        This is the content
    </div>
</div>

Now, say that when you want to click on a header, the content of that header is displayed / hidden:

$(".header").click(function () {
    $(this).parent(".topdiv:first").find(".somecontent").toggle();
});

This way, only the content of the particular header will be toggled, and not the rest.

Now you can analyze the code I have written for you, and apply it in your own context.

Andreas Grech
So this is going to work by when you click .header, it will take that element and the parent of div.topdiv and find .somecontent and then apply the toggle? Correct?Thanks for your help!Ryan
Coughlin
Yes, it will find the clicked header, traverse to it's parent div that has a 'topdiv' class, then traverse to the child element of that 'topdiv' which has a class of 'somecontent', and toggle it.
Andreas Grech
+1  A: 

Try this: nextAll

    $(".sidebar-header").click(function() {
        $(this).nextAll().toggle();
    });
maxnk
This seemed to do the trick! That is interesting how nextAll() works, I am still kind of confused on how that works, all siblings? I know this is kind of "broad" but, what does that exactly "mean"Thank you, Max
Coughlin
Internally jQuery uses "nextSibling" DOM element property when you are calling "nextAll". And there is a simple "while" cycle to scan all siblings after the current element. You can even explore it by yourself in jQuery sources: find the "nextAll" and then take a look at the according "dir" call :)
maxnk