David Hoerster
David Hoerster

Reputation: 28701

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!

Upvotes: 0

Views: 58

Answers (3)

Highway of Life
Highway of Life

Reputation: 24411

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

Upvotes: 1

emesx
emesx

Reputation: 12755

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

Upvotes: 0

genesis
genesis

Reputation: 50974

Use comma

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

Upvotes: 3

Related Questions