1

I have a DOM element with an event handler attached using addEventListener. By the time I want to remove it with removeEventListener, I no longer have a reference to the function that handles the event.
How can I remove the event listener, other than storing references to all event handlers globally?

I noticed that if I edit the DOM element using the Chrome developer tools, the event is no longer handled. Could a viable solution lie herein?

I'm not using and unable to use a library like jQuery in this particular situation.

Protector one
  • 6,926
  • 5
  • 62
  • 86

2 Answers2

0

Store a reference to the event handler in the DOM element:

 e.handlerToRemove = handler;
 e.addEventListener( ..., handler );

Later:

 e.removeEventListener( e.handlerToRemove );
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

You can't; you need that reference. Either store it globally like you suggest yourself, or store it in the DOM element like Aaron Digulla said.

Protector one
  • 6,926
  • 5
  • 62
  • 86