JavaScript get event with closure function? -
hi havr problem event , pass closure function want (this) varaible
i use
console.log(e);
its show undefined >> sloution best sloution :)
i try code
<script> for(var i=0;i<a.length;i++){ a[i].addeventlistener("keydown",(function(e){ return function(){ if (e.keycode == 13 && !e.shiftkey){ console.log(e); // e undefined problem console.log(i); // value no problem here //now iam use closure value //var = this; } }; })(e), false); } </script>
why not directly access e inside function
<script> a[i].addeventlistener("keydown",(function(){ var index=i; return function(e){ if (e.keycode == 13 && !e.shiftkey){ console.log(this); console.log(e); console.log(index); } }; })(), false);
Comments
Post a Comment