Combining multiple selectors with a filter

Is there a way to combine multiple selector and base filter in one query?

For instance...

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
source share
4 answers

not sure exactly what you are trying to do, but if you just wanted the innerhtml of the first element, you could 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, which works ... I suppose you are just asking for additional knowledge?

I never find the need for: first selectorbut, I would do this, for example:

$(".classA, .classB", someElement).first();
+2
source

What if you are .first()instead .filter(":first")?

Does it help?

+5
source

, , :

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

, mroe verbose, :first .

+2

:

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

A working example is here .

+1
source

Source: https://habr.com/ru/post/1771621/


All Articles