Reputation: 445
Basically trying to write something like this. Click .button and it .slideToggle(s) it's associated .box Div. Nesting them inside a container div to try different methods.
I feel stupid, this has been asked many times. I've tried at least a dozen none of which I can get to work.
Code is here.
<div class="container">
<div class="header">
<a href="#" class="toggle">TOGGLE</a>
</div>
<div class="box">
<p>Content</p>
</div>
</div>
Cheers.
Upvotes: 0
Views: 148
Reputation: 1610
There are many ways to do this, here are three (since I can give them a name)
The family method: $(this).parent().siblings('.box').slideToggle();
The closest find method: $(this).closest('.container').find('.box').slideToggle();
The next parent method: $(this).parent().next().slideToggle();
Upvotes: 0
Reputation: 6394
You need to use the .parent
method to move back into your header before trying to use .next
.
$('.toggle').click(function(){
$(this).next('.box').slideToggle("slow");
}); // .click
$('.toggle').click(function(){
$(this).parent().next('.box').slideToggle("slow");
}); // .click
Upvotes: 2