javascript - jQuery addClass click and removeClass when click again -


i have box without color. if box being click() addclass('.red') make red, , if click again box color change blue. change alternatively. , don't know how 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

try toggleclass like

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

if want toggle 2 classes red , blue use like

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

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -