[Project_owners] [JS] addEventListener function parameter

Nickolay Ponomarev asqueella at gmail.com
Wed May 31 01:53:32 EDT 2006


That's probably supposed to be:
function getHandler(arg) {
 return function(e) {
  alert(arg); // (not i as in Aaron's second example)
 }
}

for (var i = 0; i < 5; i++) {
 button.addEventListener("click", getHandler(i), false);
}

- but you're better off not using this unless you understand how it
works. (And it's really unnecessary if all you need to pass is a
simple string).

Nickolay

On 5/31/06, Aaron Boodman <zboogs at gmail.com> wrote:
> You need to pass the value that should be passed to the dynamic
> function to an intermediate function. In code:
>
> for (var i = 0; i < 5; i++) {
>   function getHandler(arg) {
>     return function() {
>       alert(i);
>     }
>
>     button.addEventListener("click", getHandler(i), false);
>   }
> }
>
> - a
>
> On 5/30/06, Nickolay Ponomarev <asqueella at gmail.com> wrote:
> > On 5/30/06, Didier Ernotte <didier at ernotte.com> wrote:
> > > Maybe?
> > >
> > > button.setAttribute("ident", ixyzt);
> > > button.addEventListener('click', function (event){
> > > foo(event.target.getAttribute("ident")}, 'false');
> > > ixyzt++;
> > >
> > > Didier
> >
> > That's correct solution, but the last param to addEventListener should
> > be a boolean, not a string. (Your 'false' gets passed as true
> > actually.)
> >
> > Nickolay
> > _______________________________________________
> > Project_owners mailing list
> > Project_owners at mozdev.org
> > http://mozdev.org/mailman/listinfo/project_owners
> >
> _______________________________________________
> Project_owners mailing list
> Project_owners at mozdev.org
> http://mozdev.org/mailman/listinfo/project_owners
>


More information about the Project_owners mailing list