you'd need to do: for(i=0;i<blah;i++) { [...] box.onclick = function(x) { return function() { alert(x); }; }(i); } where 'alert(x)' will be where you put whatever it is you actually want to do. Greg