Is there any selector string in jQuery that lets me filter a set by selecting only elements which have a certain element after them?
For example, if I have a set that looks like this:
[div, div, span, div, div, div, span, div, span, div]
And I only wanted to select the divs that precede a span, how could I do this in a single selector string if it is at all possible? Is there something like this:
$("div:preceding(span)")
It’s not a huge deal but I was just curious. Currently, I’m just looping through the set and checking each element to see if it is a div and the the element right after it (selected by using .next()) is a span, then if it is I just add it to an empty set created before I started the loop. Is this the best way to go about this?
Thanks in advance!
Try
.prev()Matches all the
divwhose next immediate sibling element is aspan.