Home >Web Front-end >JS Tutorial >How Can I Find DOM Elements Using Attribute Values?
Finding DOM Elements by Attribute Values
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");
Modern browsers support native querySelectorAll, allowing you to do:
document.querySelectorAll('[data-foo="value"]');
Check this documentation for more details: https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll
For browser compatibility information:
For obsolete browsers (IE9 and older), you can use jQuery:
$('[data-foo="value"]');
The above is the detailed content of How Can I Find DOM Elements Using Attribute Values?. For more information, please follow other related articles on the PHP Chinese website!