Liam
Liam

Reputation: 9855

add and remove classes in order with jQuery

Im trying to say that, if my div doesnt have the class active add it. And if it does have the class active, remove it.

Ive the following, only my code adds the class, then continues the query and removes it at the end, what would the best solution be, 2 seperat click funcitons?

$('.work-showcase').click(function(){
    if ( !$(this).hasClass('active') ){
        $(this).addClass('active');
    } else {
        $(this).removeClass('active');
    };
});

Upvotes: 2

Views: 124

Answers (2)

ravisolanki07
ravisolanki07

Reputation: 647

$("#YourID").removeClass('ClassName'); $("#YourID").addClass('ClassName');

Upvotes: 0

VisioN
VisioN

Reputation: 145408

Use toggleClass method:

$(".work-showcase").click(function() {
    $(this).toggleClass("active");
})

Upvotes: 7

Related Questions