Could someone please help me this problem I am having, I want to be able to change the background of the body on click on a bunch of list items. The body itself always has two classes that can't be removed.
HTML:
<body class="bodyclass1 bodyclass2"> <ul> <li class="red"><div class="colorme">red</div></li> <li class="blue"><div class="colorme">blue</div></li> <li class="green"><div class="colorme">green</div></li> <li class="brown"><div class="colorme">brown</div></li> </ul> </body> Css:
body.red{ background: red } body.blue{ background: blue } body.green{ background: green } body.brown{ background: brown } JQuery:
<script language="javascript"> $.noConflict(); jQuery( document ).ready(function( $ ) { $( ".colorme" ).on('click',function(){ var allKleur = $('.red, .blue, .green, .brown'); var colorDad = $(this).parents('li:eq(0)').attr('class'); $('body').removeClass(allKleur).addClass(colorDad); }); }); </script> What happens with this code is ti does add the class from the variable of the parent clicked on, but it doesn't remove the classes, leaving it just with the one that was clicked on.
It did work this way:
$('body').removeClass().addClass(colorDad); but then it removed all the classes the body tag already has, and which shouldn't be removed.
Any help in the right direction would be much appreciated, thanks.