cvall
cvall

Reputation: 25

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);
});

Upvotes: 1

Views: 35

Answers (1)

Satpal
Satpal

Reputation: 133453

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);

Upvotes: 1

Related Questions