First, note that each() loop is redundant, as you can access the data-color attribute directly in the click handler.
Secondly, to achieve what you need, you can provide a list of all classes that will be removed with removeClass , before adding a new class with addClass . Try the following:
$(".colors span").click(function() { $("body") .removeClass('blue red green orange carrot violet pink gold') .addClass($(this).data("color")); });
Updated script
However, this can be a little difficult if colors are added or removed. A better option would be to call one function to set the classes in the body element whenever an option is selected or a color is pressed. Try the following:
$(".skins, .layout").change(applyClasses); $(".colors span").click(function() { $(this).addClass('active').siblings().removeClass('active'); applyClasses(); }); function applyClasses() { var classes = [ $(".skins").val(), $(".layout").val(), $(".colors span.active").data('color') ]; $('body').removeClass().addClass(classes.join(' ')); } applyClasses();
Updated script
source share