rihanna
rihanna

Reputation: 21

Jquery click function question

I have a function that when user click first it will add a class then in second click it will remove the class, and again.. so on.. it's like when first click it means active, then second is not active. Just like that.

Here is my code:

$('.sub-nav-add').css('display','none');
   $('.nav-add').click(function(){
   $('.sub-nav-add').slideToggle(200);
   $('.box-add').addClass('sub-nav-active');
});

How can I add a second click function that will removeClass?

Upvotes: 2

Views: 113

Answers (2)

James Kovacs
James Kovacs

Reputation: 11661

Use toggleClass.

$('.sub-nav-add').css('display','none');
$('.nav-add').click(function(){
    $('.sub-nav-add').slideToggle(200);
    $('.box-add').toggleClass('sub-nav-active');
});

Upvotes: 6

PleaseStand
PleaseStand

Reputation: 32082

Take a look at the .toggle method. It will do exactly what you want.

Upvotes: 4

Related Questions