I have the following snippet of code and it works fine when I click on different elements but as long as I click an image I have clicked before nothing changes.
Here is the code:
$("img").on('click', function() {
var $body = $("body");
if($(this).data('color') == 'black') {
$body.addClass("black");
}
if($(this).data('color') == 'blue') {
$body.addClass("blue");
}
if($(this).data('color') == 'silver') {
$body.addClass("silver");
}
if($(this).data('color') == 'white') {
$body.addClass("white");
}
if($(this).data('color') == 'yellow') {
$body.addClass("yellow");
}
});
So now I want to remove the latest applied class in order to append the new one. Is there any way to do so with jQuery? Or is there any easier solution than the one I'm thinking about?
Thank you for your help.
UPDATE:
Body
has already classes so the removeClass() is not the proper way because these ones will be lost!