views:

55

answers:

3

How to write a jquery selector to match an element has attribute a or attribute b. It must match three elements below

<a a="123" b="345"></a>
<a a="123"></a>
<a b="345"></a>
+5  A: 

You can use a multiple selector (,) with the attribute-equals selector (or any other selectors), for example:

$("a[a=123], a[b=345]")

You can test it out here.

Nick Craver
I think 123 and 345 shown in the above questions are just dummy values.
Salman A
@Salman - You may certainly be correct, in that case use [@Nox's answer](http://stackoverflow.com/questions/3890265/jquery-how-to-match-an-element-has-attribute-a-or-attribute-b/3890283#3890283), if they weren't and you need to match the values, the above is how to do that.
Nick Craver
Why I can't find out that way?
complez
@complez - I don't understand the comment, can you clarify?
Nick Craver
+4  A: 

Try this

$('a[b="345"],a[a=123] ')

See the jQuery Multiple selector docs.

André Gadonski
+1 for reference
BrunoLM
+5  A: 

Or generally for mere presence of attribute

$("a[a], a[b]")
Nox
+1 - Also a very valid answer, now that you posted this the question seems much more ambiguous.
Nick Craver