Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selectors: logical OR

I can't seem to see the forest for the trees right now (Looked at the api docs already).
It has to do with jQuery selectors: What I'm trying to do is to select all elements that have class subtitle or headsection. Something like this: $('.headsection || .subtitle');

To be more specific: I want to use this selector with the function nextUntil.

$content = $some_elements.eq(0).nextUntil('.headsection || subtitle');

As far as I know || is not available in jQuery's selectors. So what's the best way to accomplish that?

Thanks for your help!

like image 212
RamboNo5 Avatar asked Mar 07 '10 14:03

RamboNo5


People also ask

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

How can use multiple ID selectors in jQuery?

Approach: Select the ID's of different element and then use each() method to apply the CSS property on all selected ID's element. Then use css() method to set the background color to pink to all selected elements. Display the text which indicates the multiple ID selectors.

How jQuery selectors are executed?

If dealing with more than two selectors in a row then your last selectors are always executed first. For example, jQuery will first find all the elements with class “. list” and then it will select all the elements with the id “second”.


6 Answers

It's the same as in CSS selectors:

$('.headsection, .subtitle');
like image 180
BlackAura Avatar answered Oct 07 '22 07:10

BlackAura


What about: $some_elements.eq(0).nextUntil('.headsection, .subtitle');

Works for me at least. Read about multiple selectors.

like image 36
Felix Kling Avatar answered Oct 07 '22 06:10

Felix Kling


Just separate them with a comma:

$content = $some_elements.eq(0).nextUntil('.headsection, subtitle');
like image 40
Justin Ethier Avatar answered Oct 07 '22 07:10

Justin Ethier


You don't really need a logical OR as such, $('.headsection, .subtitle') should do the job.

like image 37
Steve Avatar answered Oct 07 '22 05:10

Steve


jQuery uses the CSS selector syntax, so if you know that, just put the same selectors into jQuery and bob's your metaphorical uncle. jQuery uses the sizzle selector engine which supports virtually all CSS3 selectors :)

As everyone has already said - the way to do it is this: $content = $some_elements.eq(0).nextUntil('.headsection, subtitle');

like image 42
iblamefish Avatar answered Oct 07 '22 05:10

iblamefish


Would the multiple selector work for what you're doing? Api doc is here.

like image 42
Eric Avatar answered Oct 07 '22 06:10

Eric