prev() with split siblings

prev() with split siblings


Could anyone tell me why this doesn't work please?

<section class="whatever" />
<section class="whatever" />
<section class="whatever" />
<section class="whatever active" />


It should return the section before the div but it doesn't seem tofind it , however theoretically are all the sections not all siblings ofthe same parent (in this case the body)

I had to use$('').prevAll('.whatever:first'), but Idon't know why

(I'm assuming in which case prevAll returns the siblings in reverseorder, as it seemed to work as required.... Slightly different examplethan above but layout the same )


update... Seems to be a common misunderstanding so I thought I'd leave my question & solution for anyone looking, as it appears prevAll would ben be the correct approach from what else I've read. Note the docs for prev() do say this:

"If no previous sibling exists, or if the previous sibling element does not match a supplied selector, an empty jQuery object is returned"

ie my div is essentially also a sibling but doesn't not match the selector . I had wrongly assumed prev() would skip over it, and find the first instance that did match.