tags:

views:

37

answers:

4

Is there a way to combine a multiple selector and a basic filter within a single query?

For example...

var jq = $(someElement);

// Want to find the first following sibling node which has either classA or classB.

// This imaginary jquery does not quite work.
jq.nextAll("(.classA, .classB):first") 

// This works. But I wonder if I can achieve the same result with just one query.
jq.nextAll(".classA, classB)").filter(":first")
+3  A: 

what if you do .first() instead of .filter(":first") ?

Does that help ?

Taras B
+1  A: 

Well im pretty sure this is the same thing and uses only one query:

jq.nextAll('.classA:first, .classB:first');

Obviously its mroe verbose but unless there is something special about the :first selector that should work.

prodigitalson
I considered suggesting this... Does it return one result, or the first result that matched either class (two results)? I wasn't really sure so i held off but i'm curious to know if this does in fact work and I don't feel like testing :-P
Andy Groff
This will (potentially) return two results. http://jsbin.com/igufu3/
patrick dw
Ill go with what patirick says since i didnt test and have never used myself :-)
prodigitalson
This will return a collection with the first element matching `.classA` and the first element matching `.classB` (if there is one). See: http://jsfiddle.net/vnDwn/
Rob Sobers
Interesting, I sort of thought it would produce a selection with up to two elements. Its a good effort though.
Andy Groff
This is close, but not exactly what I was looking for. I'd like to get a single element or not element if there is no match.
Kei
+1  A: 

Try this:

var first = jq.nextAll('.classA, .classB').first();

Working example here.

Rob Sobers
+1  A: 

not sure exactly what you're trying to do, but if you just wanted the innerhtml of the first element you should be able to do something like:

$(".classA, .classB", someElement).html();

since html will act on the first matched element.

Otherwise, i don't see what is wrong with your method that works... I suppose you're just asking for additional knowledge?

I never find much need for the :first selectorbut i would probably do this like:

$(".classA, .classB", someElement).first();
Andy Groff
Thanks for the answer, Andy. If there are multiple matched elements with the first query, does jquery find all the matches and pick the first? I kind of thought that doing it in a single query will let jquery know that I'm only interested in the first element, so that jquery will stop looking for any other match - making the query faster. Maybe? Maybe not?
Kei
Thats interesting. I tried looking through the jQuery source but not well enough to understand exactly what is going on. What I did find is that the first function is just an alias of the eq function, which calls `this.slice( i, +i + 1 )` where `this` is the result. So, i'm assuming that no matter how you word these types of complicated selectors, its grabbing all the results and then filtering them down. Thats just speculation from what I could see though. I'm really not sure how it works.
Andy Groff
@Kei There's no way to tell jQuery to stop when it finds the first match at the moment. See: http://stackoverflow.com/questions/3950888
Rob Sobers
@Rob Thanks for the link. That answers what I was wondering. So, even if jquery supported a combined query like the one I imagined, there will be no performance gain at all. Maybe that's one of the reasons why jquery does not support something like this. I'll stick with using first() and move on.
Kei