jqueryjquery-selectorsjquery-attributes

How do I perform an OR search on jQuery attributes?


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!


Solution

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