Richard
Richard

Reputation: 1464

jQuery, adding and removing a class on click

Here's my code so far:

$('.faq dd').hide();
$('.faq dt').click(function(){
    $(this).next().slideToggle('normal');
});

I would like the DT to include an "active" class whenever someone clicks on it and then an "inactive" class whenever someone clicks on it again.

How is that possible?

Upvotes: 0

Views: 56

Answers (2)

Flo Schild
Flo Schild

Reputation: 5294

With .toggleClass, you can toggle a class. Why would you play on 2 classes ?

$('.faq dd').hide();
$('.faq dt').click(function(){
    $(this).toggleClass('active').next().slideToggle('normal');
});

Upvotes: 5

Adil
Adil

Reputation: 148110

You can use toggleClass, first assign inactive class to dt element and give active in toggle class. It will switch between inactive active class for dt.

$('.faq dd').hide();
  $('.faq dt').click(function(){
     $(this).toggleClass('active');  
     $(this).next().slideToggle('normal');
});

Upvotes: 0

Related Questions