JavaScript Event Handler Closure


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. window.onload = function() {
  2. var ul = document.getElementById("foo");
  3. var lis = ul.getElementsByTagName("li");
  4. for(var i=0; i<lis.length; i++) {
  5. lis[i].onmouseover = function(x) {
  6. return function() {
  7. console.log(x);
  8. }
  9. }(i);
  10. }
  11. };

URL: http://jsfiddle.net/YwtEz/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.