Tuesday, April 06, 2010

YUI 3: Looping through an array of elements

OH MAN... I was about ready to kill somebody over this one. Being a die-hard YUI2'er, the concepts introduced in YUI 3, while possibly familiar to the jQuery crowd, are COMPLETELY foreign to us YUI 2 jedis.

In any event, the how to...

------------------

YUI 2


var _d = YAHOO.util.DOM;
var aElements = _d.getElementsByClassName('className');
var i = aElements.length;

while(i--){
    doSomething(aElements(i));
}

------------------

YUI 3


YUI().use('node', function(Y){
    var aElements = Y.all('div.selector');

    var i = aElements.size();
    while(i--){
        doSomething(aElements.item(i));
    }
}
------------------

Note the "size" call as opposed to "length" and the "item(i)" in the "doSomething" call. Ouch.

I love YUI and have been a huge fan of Crockford, Glass, Eric, Nate and the rest of the crew over there but DUDES! C'MON! jk ;)

I finally came to grips after finding the "Rosetta Stone." Have a look and good luck!

No comments:

Post a Comment