2

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?

2 Answers 2

6

Use toggleClass.

$('.sub-nav-add').css('display','none');
$('.nav-add').click(function(){
    $('.sub-nav-add').slideToggle(200);
    $('.box-add').toggleClass('sub-nav-active');
});
Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

Man, how have I never seen that before? That is really convenient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.