My moreSelectors plugin has an undocumented feature that enhances
the .siblings() method to optionally return preceeding or following
siblings only.

Syntax: $(...).siblings(expr, self, prevOrNext)
Specify self as true to retain the current element(s) in the results.
(default is false so that existing functionality is unaffected)
Specify prevOrNext as "prev" to only include previous siblings, or
"next" to only include following siblings. (Also accepts false or true
instead of "prev" and "next" respectively) (default is "undefined" so
that existing functionality is unaffected)

To get all the SPANs that follow the DIVs you could try something
like:  $("DIV").siblings("SPAN", false, "next")

BUT, you also want to filter by "first" and "second" etc so you need a
filter too. I think you'll have to build up the $ list manually,
something like this (untested):
var $spans = $()
$("DIV").each(function(){
    $spans.add( $(this).siblings("SPAN:contains(" + $(this).text() +
"):first", false, "next")
});

I find this solution rather flaky so I would urge you use a stronger
technique for identifying the SPANs if possible, such as by classname
(that is, assuming you have the option of changing the html).

George

PS: My enhancements to the siblings() method are not well tested,
hence the lack of mention in the docs!
http://www.softwareunity.com/sandbox/JQueryMoreSelectors/

> my goal is, for each "div", selecting the corresponding subsequent "span" so
> that first/first and second/second

Reply via email to