Possible Duplicate:
Javascript closure inside loops - simple practical example
I am (quite obviously) a beginner in javascript. I am attempting to understand why, when using a for loop and calling an event handler ala
var nav = document.getElementById('nav');
var navLinks = nav.getElementsByTagName('a');
var content = document.getElementById('content');
var contentSections = content.getElementsByTagName('div');
for(i =0; i < contentSections.length; i++) {
contentSections[i].style.display = 'none';
}
for(i =0; i < navLinks.length; i++) {
navLinks[i].onmouseover = function() {
contentSections[i-1].style.display = 'block'
}
}
I only get the last iteration of the loop. How would I call a function to act on each of the links in the navLinks array as they are moused over?
There are many questions on this, but often times they are a bit too complicated for me to understand.