There’s a simple for loop, can anyone explain to me why isn’t it working?
Here’s the code (it’s there in the fiddle too)
JS –
var anchors = document.getElementsByTagName('a');
for (var i, anchor; anchor = anchors[i]; i++) {
console.log('looping');
anchor.addEventListener("click", setNavigation, false);
}
HTML –
<a href="#"></a>
<a href="lol"></a>
You forgot to initialize
ito the value0. Usefor (var i=0, ….Btw, usually you check for
ito be smaller than thelengthof the iterated collection instead of looking for truthy values in the loop-condition. Then you also wouldn’t need that additional variableanchor: