cvall cvall - 1 year ago 60
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);


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

Directly pass the variable to your method


OR, using string concatenation

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