I'm trying to get multiple divs with the class 'box' to switch between three classes: 'box', 'box black', and 'box blue' when I click on them. Something isn't quite working with how I have it set up. Thanks for your help!
$('.box').click(function () {
if ($(this).class == 'box') {
$(this).addClass('black');
} else if ($(this).class == 'box black') {
$(this).removeClass('black');
$(this).addClass('blue');
} else if ($(this).class == 'box blue') {
$(this).removeClass('blue');
}
});
.hasClass()instead of.classto check whether the element has that class or not!ifcondition and will addClass black to the clickeddiv!