Kyle
Kyle

Reputation: 4376

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.

Upvotes: 5

Views: 113

Answers (2)

Guffa
Guffa

Reputation: 700910

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"]')

Upvotes: 2

zerkms
zerkms

Reputation: 255155

$('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"]').

Upvotes: 10

Related Questions