I have a list of divs, that I want to add "onmouseover" events to. I try to iterate over the list of elements, and redefine the "onmouseover" function like this:
for (var i=0; i<3; i++) {
elements[i].onmouseover = function() { alert('FOO') };
}
Unfortunately, after the page finishes loading, it looks like this only gets applied to the very last element. I'm sure this is a real newbie question, but I still haven't figured it out...
Here's a better example exhibiting my problem: http://jsfiddle.net/qajPM/
jquery
, it'll help you a lot.elements
array created? Is your code in an onload handler, or at the end of the body after all the divs in question?