Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: slideToggle() multiple divs independently using one function

Markup:

<div>
    <h3 class = "trigger">Heading 1</h3>
    <ul class = "toggle">
        <li>Line One</li>
        <li>Line Two</li>
        <li>Line Three</li>
    </ul>
</div>
<div>
    <h3 class = "trigger">Heading 2</h3>
    <ul class = "toggle">
        <li>Line One</li>
        <li>Line Two</li>
        <li>Line Three</li>
    </ul>
</div>

and the JQuery:

$(".toggle").slideUp();
$(".trigger").click(function(){
    $(".toggle").slideToggle("slow");
  });

I'd like to be able to toggle these <ul>s independently. Currently, when either <h3> title is clicked both divs toggle...

code is at: http://jsfiddle.net/CPvCZ/4/

like image 906
starsinmypockets Avatar asked Jan 24 '26 08:01

starsinmypockets


1 Answers

The solution is quite simple:

$(".toggle").slideUp();
$(".trigger").click(function(){
    $(this).next(".toggle").slideToggle("slow");
  });

http://jsfiddle.net/CkTRa/

like image 82
methodofaction Avatar answered Jan 26 '26 22:01

methodofaction