Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  77] [ 3]  / answers: 1 / hits: 48898  / 13 Years ago, tue, december 13, 2011, 12:00:00

I want to attach a function on every link on the site to change a parameter.



How can I do this without jQuery?



How do I traverse every link (it might be a DOM item) and call a function on them?


More From » events

 Answers
37

getElementsByTagName is supported by all modern browsers and all the way back to IE 6



var elements = document.getElementsByTagName('a');
for(var i = 0, len = elements.length; i < len; i++) {
elements[i].onclick = function () {
// stuff
}
}

[#88578] Monday, December 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payton

Total Points: 307
Total Questions: 96
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
;