jQuery select by attribute using AND and OR operators jQuery select by attribute using AND and OR operators jquery jquery

jQuery select by attribute using AND and OR operators


AND operation

a=$('[myc="blue"][myid="1"][myid="3"]');

OR operation, use commas

a=$('[myc="blue"],[myid="1"],[myid="3"]');

As @Vega commented:

a=$('[myc="blue"][myid="1"],[myc="blue"][myid="3"]');


Simple use .filter() [docs] (AND) using the multiple selector [docs] (OR):

$('[myc="blue"]').filter('[myid="1"],[myid="2"]');

In general, chaining selectors, like a.foo.bar[attr=value] is some kind of AND selector.

jQuery has extensive documentation about the supported selectors, it's worth a read.


How about writing a filter like below,

$('[myc="blue"]').filter(function () {   return (this.id == '1' || this.id == '3');});

Edit: @Jack Thanks.. totally missed it..

$('[myc="blue"]').filter(function() {   var myId = $(this).attr('myid');      return (myId == '1' || myId == '3');});

DEMO