Reputation: 117
How can I achieve this with jQuery?
<a href="#" class="accordion-link">
is clicked, add class 'active'.<a href="#" class="accordion-link">
is clicked, hide active from all and add to this.<a href="#" class="accordion-link active">
is clicked, remove "active" (this last one is in the event they close the div that is currently active, it will remove the 'active' class).Upvotes: 0
Views: 166
Reputation: 1988
That should do what you want:
$(".accordion-link").on('click', function() { var $this = $(this); var wasActive = $this.is('.active');
$(".accordion-link").removeClass("active");
if (!wasActive) {
$this.addClass("active");
}
});
Upvotes: 1
Reputation: 254916
How about this:
$('.accordion-link').click(function() {
var t = $(this);
if (t.hasClass('active')) {
t.removeClass('active');
} else {
t.siblings('.active').removeClass('active');
t.addClass('active');
}
return false;
});
Upvotes: 2