Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle box closed, as other opens?

Tags:

jquery

toggle

Can someone help me out with this code, i assume its easy if you know how..?

Currently click to toggle box appears below.

I need it so when you click on the other toggle, the box currently open closes, so its basically impossible for them to be both open at the same time?

But you can still toggle each individual one open/closed as normal, just when its open the other one (if open) closes also.

So far.. http://jsfiddle.net/CkTRa/380/

Thank you.

UPDATE:

Can it work with targeting independent boxes like so...?

HTML:

<div>
<h3 class = "trigger"> <a href="#box1">Heading 1</a></h3> 
</div>
<div>
<h3 class = "trigger"><a href="#box2">Heading 2</a></h3>
</div>

<div class = "toggle" id="box1">
    box one content
</div>
<div class = "toggle" id="box2">
    box two content
</div>​

jQuery so far:

$(".trigger").click(function(){
    $(this).next(".toggle").slideToggle("slow");
});
like image 887
user4630 Avatar asked May 10 '26 02:05

user4630


1 Answers

I think this simple code should work perfectly

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

See jsFiddle demo

like image 124
void Avatar answered May 12 '26 06:05

void