Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector with "and" and "or"

Can somebody help me with the syntax of what I'm trying to do here?

jQuery('(div[data-positiontype="3"]) && (div[data-positiontitle*="test"] || div[data-positiondesc*='test'])')

Basically I need the ability to select all divs that have a particular "data-positiontype" attribute and a token in either the "data-positiontitle" attribute or the "data-positiondesc" attribute.

like image 886
Kyle Avatar asked Sep 27 '12 20:09

Kyle


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 can I select an element with multiple classes in jQuery?

The . class selector in jquery is used to select multiple classes. Parameter: class: This parameter is required to specify the class of the elements to be selected.

Can you use a variable in a jQuery selector?

Yes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.


2 Answers

$('div[data-positiontype="3"]').filter('[data-positiontitle*="test"], [data-positiondesc*="test"]').

or the straightforward:

$('div[data-positiontype="3"][data-positiontitle*="test"], div[data-positiontype="3"][data-positiondesc*="test"]').
like image 97
zerkms Avatar answered Oct 16 '22 20:10

zerkms


You stack the selectors together to find elements that matches both, and use the , combinator between selectors:

jQuery('div[data-positiontype="3"][data-positiontitle*="test"],div[data-positiontype="3"][data-positiondesc*="test"]')
like image 38
Guffa Avatar answered Oct 16 '22 20:10

Guffa