Removing an anonymous event listener

erikvold picture erikvold · Jun 24, 2010 · Viewed 30.6k times · Source

Is there anyway to remove an event listener added like this:

element.addEventListener(event, function(){/* do work here */}, false);

Without replacing the element?

Answer

Joe picture Joe · Jun 25, 2010

There is no way to cleanly remove an event handler unless you stored a reference to the event handler at creation.

I will generally add these to the main object on that page, then you can iterate and cleanly dispose of them when done with that object.