How do I perform an OR search on jQuery attributes?

60 views Asked by At

I have a bunch of li items that have some custom attributes:

<ul>
  <li count="1">foo</li>
  <li count="1">foo1</li>
  <li count="*">foo2</li>
  <li>foobar</li>
  <li count="0">bar2</li>
  <li count="*">bar3</li>
</ul>

I'd like to select those li elements that have an attribute count with a value of 1 or 0. Can I do this without performing 2 selections?

I can see how I can perform an AND selection in order to narrow down results (e.g. $("li[count='0'][otherAttr='blah']")...). But I'm not sure if you can perform an OR selection.

Thanks in advance for your help!

3

There are 3 answers

3
Highway of Life On BEST ANSWER

You can use a number of ways, but you'll need to use a comma:

$("li[count='0'], li[count='1']")...

Or, my preferred method...

$("li").filter("[count='1'], [count='0']")...
0
genesis On

Use comma

 $("li[count='0'], li[otherAttr='blah']")....
1
emesx On

Can't you use li[count*='01'] ?