Monday 15 February 2010

jquery - Add/remove class on click to change background color of a div -


Clicking next to me I'm trying to get a little black div to change the color to green It should continue to be done

Unfortunately this does not seem to work and I can not find my answer through checking other topics on this.

My HTML:

  & lt; Div id = "block" & gt; & Lt; / Div & gt;   

My CSS:

  div {width: 100px; Height: 100 pixels; Background color: black; }   

My jQuery:

  $ (function () {$ ("# block"). Click (function () {if ($ ( This "." ("Fade" And {$ (this). CSS ("background-color", "black", function () {$ (this) .removeClass ("fade");})}}});}); Actually I am trying to check whether my block has a certain class, if not add it and change the background color to green in my CSS if it contains There is a class (this happens when it is green), remove it and turn the background color back into black in my CSS.  

JSfiddle:

Thanks in advance!

Hope this helps Bella:

  $ (Function () {$ ("# block"). (Function () (if (!! (!!) .hasClass ("greenDiv")) {$ (This) .addClass ("greenDiv");} and {$ (This) .removeClass ("greenDiv");}});});    

No comments:

Post a Comment