for (var i = 0; i < domElems.length; ++i) { domElems[i].onclick = clickHandler(i); } function clickHandler(n){ return function(){ console.log(n); }; }