[Project_owners] [JS] addEventListener function parameter

Neil neil at parkwaycc.co.uk
Wed May 31 02:07:14 EDT 2006


Michael Bar-David wrote:

> I'm trying to dynamically add buttons to the page, and giving them 
> numbers:
> when clicking button1 it should use a function, and give it 1 as 
> parameter, the second is button2, an should pass 2 as parameter, and 
> so on.
> this code make problems (I can't define what):
>
> button.addEventListener('click', function (){ foo(ixyzt)}, 'false');
> ixyzt++;
>
> (ixyzt is a global variable)

Another approach that Gecko supports is to use a handler object:
function fooListener(arg) {
    this.arg = arg;
}
fooListener.prototype.handleEvent = function handleEvent(event) {
    foo(this.arg);
}
button.addEventListener('click', new fooListener(ixyzt++), false);


More information about the Project_owners mailing list