I am trying to get an element’s index number while onmouseover.
I can do this easily with jQuery but native javascript is bulletproof /:
var ele = document.getElementsByClassName('sample')[0];
for(i=0; i<ele.children.length;i++){
ele.children[i].onmouseover = function() {
this.style.background='red';
alert(i);//can't get the value
}
}
<ul class="sample">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
That’s because
iwas overridden already, store it in an attribute:DEMO
Or use a closure:
DEMO