cvall cvall - 7 months ago 18
Javascript Question

Add various classes with jquery, one of them being a var

This keeps on throwing an error, is there a way to put two or more classes in the same "bracket" when one is a var?


$("#test a").click(function(){
var $color = $(this, "p").text()
$("#left-part").removeClass().addClass("ClassnameX" $color);
});

Answer

There is syntax error in the .addClass("$color" $color)

Directly pass the variable to your method

 $("#left-part").removeClass().addClass("$color").addClass($color);

OR, using string concatenation

$("#left-part").removeClass().addClass("$color " + $color);
Comments