I am attempting to create a box that changes color when clicked. When the box is first clicked, it will turn red by adding the class red
, and if clicked again, it will change to blue. The colors alternate with each click, but I am unsure of how to achieve this effect.
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');
});