Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  198] [ 7]  / answers: 1 / hits: 62598  / 10 Years ago, thu, april 24, 2014, 12:00:00

Let I've some elements with href=/href_value/attribute. How to get all elemenets such that their href attribute has the href_value value?


More From » javascript

 Answers
2

Heres a version that will work in old and new browsers by seeing if querySelectorAll is supported



You can use it by calling getElementsByAttribute(attribute, value)



Here is a fiddle: http://jsfiddle.net/ghRqV/



var getElementsByAttribute = function(attr, value) {
if ('querySelectorAll' in document) {
return document.querySelectorAll( [+attr+=+value+] )
} else {
var els = document.getElementsByTagName(*),
result = []

for (var i=0, _len=els.length; i < _len; i++) {
var el = els[i]

if (el.hasAttribute(attr)) {
if (el.getAttribute(attr) === value) result.push(el)
}
}

return result
}
}

[#71314] Tuesday, April 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierceabnerc

Total Points: 430
Total Questions: 92
Total Answers: 102

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;