charlie
charlie

Reputation: 1384

Collapse all other divs when different div is expanded

I have this JS Code to slide DIVs up and down:

$(".desc_div").slideUp();
$(".open_div").click(function(){
    $(this).next(".desc_div").slideToggle("slow");
  });

how can i make it so when a second div is expanded the other open one will close?

http://jsfiddle.net/MaW35/

Upvotes: 0

Views: 1222

Answers (2)

Kumar Akarsh
Kumar Akarsh

Reputation: 4980

This is working fine:

$(".desc_div").slideUp();
$(".open_div").click(function(){
    $(this).next(".desc_div").slideToggle("slow");
    $('.desc_div').hide()
    $(this).next(".desc_div").show()
  });

Upvotes: 0

Sergei Zahharenko
Sergei Zahharenko

Reputation: 1524

use .siblings() - it picks all neighbours of element excluding itself.

$(".open_div").click(function(){
    $(this).next(".desc_div").slideToggle("slow").siblings('.desc_div').slideUp();
});

http://jsfiddle.net/acrashik/MaW35/7/

Upvotes: 2

Related Questions