Question

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!!

Était-ce utile?

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

Autres conseils

check this out

<div id="foo" class="bar">foo</div> 

<script type="text/javascript">   

 var addClass = function(level) { 
   YAHOO.util.Dom.addClass('foo',"level"+ level ); 
 };

 addClass(3); 
</script> 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top