0

Apologies I didnt really know how to title this post.

I have the following 3 functions in YUI. What I do is look for a div with classname .box2, .box3, or .box4, and then add another class to all those divs ie the .box2 div gets another div .2

The code below works but I'm too much of a noob to get it all combined into one function for all values of the var number instead of writing it out 3 times like this.

var number = 2;
var nodes = Y.all(".box"+ number);

nodes.each(function(node){  

    node.addClass(number);

});



var number = 3;
var nodes = Y.all(".box"+ number);

nodes.each(function(node){  

    node.addClass(number);

});



var number = 4;
var nodes = Y.all(".box"+ number);

nodes.each(function(node){  

    node.addClass(number);

}); 

Thanks a million!

user1525612
  • 1,864
  • 1
  • 21
  • 33

2 Answers2

2

You don't have to do each. Just use addClass function on Y.all(...). Like this...

var arr = [2, 3, 4];
var query = ".box" + arr.join(", .box");
Y.all(query).addClass(levelnumber);
mohkhan
  • 11,925
  • 2
  • 24
  • 27
  • thanks mohkan, but then how do I declare the value of the variable 'number'? how does it know to do that fucntion for all values of number 2 to 4? – user1525612 Aug 04 '13 at 23:45
  • thanks alot mohkan, i will need this bit of code in future. wish i could mark more than one answer correct on here :( – user1525612 Aug 05 '13 at 00:49
1

What about this?

var numbers = [2, 3, 4];

for(var i = 0; i < numbers.length; i++)
{
  var boxnum = numbers[i];
  Y.all(".box"+boxnum ).addClass(boxnum);  
}

http://jsfiddle.net/854Th/

danbroooks
  • 2,712
  • 5
  • 21
  • 43