/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// a common mistake, this will not work as expected for (i ...) { var div_id = divs[i].id; divs[i].onmouseover = function () { show_element_id(div_id); }; } // Instead use a function to wrap the actual // function call. This takes advantage of closures // to remember the div_id value. for (i ...) { var div_id = divs[i].id; divs[i].onmouseover = function (id) { return function () { show_element_id(id); }; }(div_id); }