Simple closure:
var foo = "bar"; e.addEventListener('whee',function(evt){ iRespond(evt,foo); },false);
If a simple closure fails (because you snap a variable that changes, for example, in a loop), you need to create a new closure on this value:
foo.addEventListener('bar',(function(jim){ return function(evt){ iRespond(evt,jim);
For instance:
var buttons = document.querySelectorAll('button'); for (var i=buttons.length;i--;)} buttons[i].addEventListener('click',(function(j){ return function(){ alert("You clicked on button #"+j); }; })(i),false); }
You can choose a variable name both inside and outside the function without permission - using i instead of j above - but you may run into confusion.
source share