Earlier quoted context omitted.
this is what I usually tell people to do: var els = document.getElementsByTagName('li');//use var unless you really really really meant global for(var i=0; i
Does this work (from a non-JS person -- I like reading JS books, but don't code in it at all). This is certainly less elegant, but seems easier to follow: var els = document.getElementsByTagName('li'); for(i=0; i
Your confusion is because of a javascript quirk
for (...) {
var foo = bar
}
is actually the same as var foo
for (...) {
foo = bar
}
Notice how you only ever have one fooWhat you need is:
for (...) {
(function(bar) {
var foo = bar
})(bar)
}
which create a new foo every time you loop