tags:

views:

49

answers:

4

Hello i have an HTML Code like this

<div id="top">
  <div class="panel">
    <h1>toppanel</h1>
    <p>Content from toppanel</p>
  </div>
  <div class="panelholder" id="ptop">
    <a class="open" href="#">open</a>
    <a class="close" href="#">close</a>
  </div>
</div>

for more than on time. The only differnce is the id from the first div My jQuery Code looks like this

// Expand Panel
$(".open").click(function(){
    $(#######).slideDown("slow");   
}); 

and my question is what i have to write for $(#######), if I want to slideDown the div with the class panel

A: 
$(".open").click(function(){
    $(this).parent('div.panel').slideDown("slow");   
});

This will find the parent of the open button with the panel class and slide it down, that way you don't have to worry about IDs or anything, just the open link being inside a panel.

Parrots
I don't think the 'div.panel' is actually a parent of '.open'...
John Fisher
+3  A: 
// Expand Panel
$(".open").click(function(){
    $(this).parent().siblings('.panel').slideDown("slow");
});

You should read the API documentation on traversal.

jonwd7
Does closest check siblings too? '.panel' isn't a parent of '.open'
John Fisher
.panel *is* a sibling of .open's parent, however. Which is what my code is doing.
jonwd7
When I saw it, you were using`.closest('div.panel')`
John Fisher
Erm, I haven't edited my answer and would never use `div.panel` because I generally keep my selectors tag-agnostic.
jonwd7
Strange. Somehow, my comment got attached to the wrong answer, then.
John Fisher
+2  A: 
$(this).closest('top').find('.panel').slideDown("slow");

or

$(this).parent().prev().slideDown("slow");
John Fisher
A: 

Edited to a simpler solution!

$(this).parent().siblings(".panel")

Have a look at the docs on traversing.

Fiona Holder
I think there's one too many '.parent()' calls in there.
John Fisher
Changed it, this one is better!
Fiona Holder