I have a list of 3 links, that I wanna iterate through, so each one of them can do something.
But when I use the for-loop, it only gives me 3
in the console, which is the number of links in the list.
I wanna the console to show each of them like so: 0, 1, 2
;
Also how can I get the index position of each of the links?
See code here: http://jsfiddle.net/c8Wdj/
No jQuery or any library please...
JavaScript:
(function(){
var triggers = document.getElementById('some-list').getElementsByTagName('a');
for (var i = 0, max = triggers.length; i < max; i += 1) {
triggers[i].addEventListener('mouseenter', function(e) {
console.log(i);
}, false);
}
}());
HTML:
<ul id="some-list">
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
</ul>