I'm thinking about, if it is possible in jQuery to select elements by named attributes using AND and OR.
Example:
<div myid="1" myc="blue">1</div>
<div myid="2" myc="blue">2</div>
<div myid="3" myc="blue">3</div>
<div myid="4">4</div>
I'd like to select all the elements where myc="blue"
but only those with myid
set to either 1 or 3.
So I tried:
a=$('[myc="blue"] [myid="1"] [myid="3"]');
but it does not work, same here:
a=$('[myc="blue"] && [myid="1"] || [myid="3"]');
Is it possible without writing special filter functions?
First find the condition that occurs in all situations, then filter the special conditions:
To properly select the elements using the logical operations that you've stated, you just need
jQuery.filter()
for theAND
operation, not "special filter functions". You also needjQuery.add()
for theOR
operation.Alternatively, it is possible to accomplish using shorthand in a single selector, where jamming selectors together acts as an
AND
and separating with a comma acts as anOR
: