views:

42

answers:

1

Hi,

I'm trying to use next() to toggle a div. The problem is that the "trigger" is in a different div than the one I want to toggle. An example that works:

$("span.trigger").click(function(){
        $(this).next("div.task_description").slideToggle("fast");
});

<span class="trigger">The trigger</span>
<div class="task_description ">
  some content
</div>

But the way I need the html setup is:

<div>
  <span class="trigger">The trigger</span>
</div>
<div class="task_description ">
  some content
</div>

That doesn't work... any suggestions?

+3  A: 

In this case you need a .parent() as well, like this:

$("span.trigger").click(function(){
  $(this).parent().next("div.task_description").slideToggle("fast");
});

Alternatively, if your nesting may change, you can go up to the <div> you're in, like this:

$(this).closest("div").next("div.task_description").slideToggle("fast");
Nick Craver
I actually have div's in-between the trigger and the toggling div... used nextAll() and everything is perfect.. Thanks!
mike
@mike - make sure to use `.nextAll(".task_description:first")` if you just want the first one of that class that follows!
Nick Craver