How to use regular javascript to view each <a> tag and modify href?
There are many tags on the page. How do I go through all of them and replace their "href" with " http://example.com "?
(do not use jQuery)
+3
5 answers
You can use the collection document.links. It is defined by W3C and is supported by all common browsers.
In addition, you get access not only to elements <a>, but also to tags <area>(which are commonly used in client image cards).
for(var i=0; i < document.links.length; i++) {
document.links[i].href = "http://example.com";
}
+4
getElementsByTagName(), , , href.
var links = document.getElementsByTagName('a');
if(links) { // if none are found, do not continue
for(var i = 0; i < links.length; i++) {
links[i].href = 'http://example.com/';
}
}
+2