How to get all elements with a specified href attribute
If you have the luxury of neglecting IE 7 or lower, you can use:
document.querySelectorAll("[href='href_value']");
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
}
}
Maybe you need to get all the elements whose href
value contain your specific href_value
? If so, try:
document.querySelectorAll('[href*="href_value"]');