I thought $('.myClass > ul > li > a') returned an array of jquery objects
That's where the misunderstanding is. It doesn't return an array of jQuery objects, it returns a single jQuery object. That object is a pseudo-array (in this case of DOM elements; jQuery objects can contain other things, but 99% of the time they contain DOM elements). So [0]
gives you a DOM element, not a jQuery object, which has no closest
method.
If you want the first one, use jQuery's first()
or eq(0)
methods, which give you the first element wrapped in a jQuery object, or if you like get the element with [0]
then use $()
to wrap it.
// first:
$('.myClass > ul > li > a').first().closest('li')
// eq(0):
$('.myClass > ul > li > a').eq(0).closest('li')
// The long way (I wouldn't do this, just be comprehensive)
$($('.myClass > ul > li > a')[0]).closest('li')
You can also use jQuery's :first
selector enhancement:
$('.myClass > ul > li > a:first').closest('li')
...but doing so means jQuery can't hand off to the browser's own query selector.