3

How can I add an event listener (addEventListener(),attachEvent()) that also accepts a parameter?

The parameter is passed as the element's custom attribute like:

<img src="icon.gif" alt="Test button" command="test" />
<img src="icon2.gif" alt="Test button2" command="write" />
Shog9
  • 156,901
  • 35
  • 231
  • 235
samuel
  • 321
  • 2
  • 13

2 Answers2

3

You can use getAttribute in your handler, something like

var param = this.getAttribute('command');

KooiInc
  • 119,216
  • 31
  • 141
  • 177
3

You could use something like this:

element.addEventListener ( 'click', (function ( myParam ) {
    return function () {
        // user myParam here
    };
} ) ( yourParam ), false );

whatever you pass in as "yourParam" will be accessible to the event handler via the "myParam" parameter ...

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • 1
    this worked for me: attachEvent('onclick', function() { linkClicked(link.getAttribute("command")); }, false); – samuel Dec 09 '09 at 12:55