tags:

views:

59

answers:

2

Hi,

Can you please tell me if there is any DOM API which search for an element with given attribute name and attribute value:

Something like:

doc.findElementByAttribute("myAttribute", "aValue");

Thank you.

+1  A: 

There's not a very short way to do this in vanilla javascript, but there are some solutions available.

You do something like this, looping through elements and checking the attribute

If a library like jQuery is an option, you can do it a bit easier, like this:

$("[myAttribute=value]")

You can also do start-with, ends-with, contains, etc...there are several options for the attribute selector.

Nick Craver
A: 

Duplicate if you are looking for a Jquery Sol:

http://stackoverflow.com/questions/696968/jquery-v1-3-2-find-element-by-attribute

Rajat