Search code examples
javascriptyui

Is there any last() in YUI NodeList?


Let's say I try to select the last child of a list of nodes in YUI3:

var node = Y.one('#node');
node.all('button').last();   // pseudo-code, not actually working!

Is there an equivalent to jquery's last() (and first(), respectively) in YUI3?

I checked the API docs and did not find anything comparable.

node.one('button:last-child')

did'nt do the job either.


Solution

  • one('button:last-child')
    

    Should work. See this example. However, :last-child is referring to the last child of its parent, so it is highly dependent on your DOM structure.

    <ul>
        <li><button>Foo</button></li>
        <li><button>Bar</button></li>
        <li><button>Baz</button></li>
    </ul>
    <script>
    Y.one('button:last-child'); // yields <button>Foo</button>
    // should be written like so
    Y.one('li:last-child button');
    </script>