user1673591
user1673591

Reputation: 79

jquery class toggle not closing previous

html

<div class="wrap">
<div class="t1">
   head 1
</div>
<div class="t2">
    hi
</div>
</div>
<div class="wrap">
<div class="t1">
head 2
</div>
<div class="t2">
    hi2
</div>
</div>

jquery

$(".t2").hide();
$(".t1").click(function(){
$(this).next(".t2").toggle();
});

I pressed first t1 then t2 is opening and when i press second t1,first t2 should close but it is still open

http://jsfiddle.net/q539mqon/3/

Upvotes: 0

Views: 71

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to hide it in the click handler

var $t2s = $(".t2").hide();
$(".t1").click(function () {
    var $t2 = $(this).next(".t2").toggle();
    $t2s.not($t2).hide();
});

Demo: Fiddle

Upvotes: 3

Related Questions