Search code examples
javascriptyuiyui3

addClass and class to be added is part name and part variable


Working with the YUI library and I have the following:

var levelnumber = 3;

I want to add a class to a div so that it says ".level3" , so the class to be added should be the word "level" plus the value of the variable.

Something like this but Im not sure on the syntax:

addClass("level"+[levelnumber]);

I have tried quite a few combinations of single and double quotation marks but cant get it to work.

Thanks!

EDIT:

Sorry fellas!

I asked the wrong question.

I have that part sorted actually, my problem is not with adding the class, it is with selecting a class!

I want to select .group .level3

var level = 3;
Y.all(".group" '.level'+[levelnumber])

That is what I have now but don't know where all the quoatation marks should go.

Thanks!!


Solution

  • Just remove the square brackets. As long as one of the variables is a string, it will concatenate them together. Here's a good article on strings in JavaScript.

    addClass('level'+ levelnumber);