jQuery addClass click and removeClass when click again

css, html, javascript, jquery

Solution

Try with `toggleClass` like

$('div').click(function() {
    $(this).toggleClass("red");
});    

If you want to toggle `2 classes` `red` and `blue` then use like

$('div').click(function() {
    $(this).toggleClass("red blue");
});

Problem

I have a box without color. if the box is being `click()` it will `addClass('.red')` to make it red, and if click again the box color change to blue. they change alternatively. and I don't know how to make it. Code HTML ``` <div class='box'></div> ``` CSS ``` .box { width: 250px; height: 100px; border: 1px #000 solid; } .red { background: red; } .blue { background: blue; } ``` Javascript ``` $('div').click(function() { $(this).addClass('red'); }); ``` link of jsfiddle

Original source