javascriptgreasemonkeyaddeventlisteneruserscripts

Removing an anonymous event listener


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

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

Without replacing the element?


Solution

  • 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.