Trying to select the last element of a list, I can't grasp the fundamentals of getElements, here is the html code
<section class="main">
<ul>
<li>Lorem ipsum dolor sit amet.</li>
<li>Consectetur adipisicing.</li>
<li>Lorem ipsum dolor</li>
</ul>
</section>
see this selectors:
main = $$('section.main') // ok
main.getElements('li').getFirst() // .getFirst is not a function
main.getElements('li').getLast() // Object [ li, li, li, li, li, li ]
main.getElements('ul').getLast('li') // Object [ ul ]
main.getElement('ul').getElements('li').getLast().getLast() // li, Works!!
getLast
should return the last element of an array, is it?
A lot of mootools functions, when applied to an array, apply that function to every element in that array.
$$ returns an Array. so 'main' is an Array
getElements('li'), when invoked on an Array, invokes itself on every element of that Array. So now you've got an Array of Arrays. (actually, they're Elements
objects, but let's ignore that)
main.getElements('li').getLast() returns you the last array in that Array of Arrays, which is why you're still getting an array instead of an element.
In other words, main.getElements('li').getLast()
is equivalent to: main.getLast().getElements('li')
.