[Project_owners] [JS] addEventListener function parameter

Neil neil at parkwaycc.co.uk
Tue May 30 16:01:23 EDT 2006


Michael Bar-David wrote:

> JS question:
>
> I'm trying to work with addEventListener, to implement this code:
>
> <input type='button' id='bt1' value='press me' onclick='foo(1)'/>
>
> with JS:
>
> document.getElementById('bt1').addEventListener('click',***);
>
> what sould I write instaed of *** ?
>
> (The problem is, how to pass the parameter to to function)

It depends on how you want to distinguish between buttons. For instance, 
if the parameter is always the third letter of the id you can write an 
intermediate function:

function onInputClicked(event) {
    foo(event.target.id[2]);
}

Alternatively you could use the button's name, or a custom attribute. 
Otherwise you would have to write a separate function for each button.


More information about the Project_owners mailing list