cvall cvall - 2 years ago 112
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download