Official docs: Event.stopObserving

The official docs don't currently mention have only just been updated to mention some changes to Event.stopObserving as of Prototype 1.6.0:

Event.stopObserving(element[, eventName][, handler])
// Unregisters an event handler.

  • element - The element to stop observing (or its ID).
  • eventName - The name of the event to stop observing. Optional; if not given, all events hooked up via Event.observe are unhooked.
  • handler - The handler to unhook. Optional; if not given, all handlers for the given event name that were hooked up via Event.observe are unhooked.

Changes in 1.6.0:

  • The eventName and handler parameters are now optional.
  • The previous useCapture parameter is no longer supported.


1. Unhook all event handlers from 'myDiv':


2. Unhook all 'click' event handlers from 'myDiv':

Event.stopObserving('myDiv', 'click');

3. Unhook a specific 'click' handler from 'myDiv':

Event.stopObserving('myDiv', 'click', myHandler);

In #3 above, you must have passed exactly the same parameters into Event.observe previously; see the notes on the official docs for details, but for instance, this does not work:
Event.observe('myDiv', 'click', this.myHandler.bind(this));
// later...
Event.stopObserving('myDiv', 'click', this.myHandler.bind(this)); // <= WRONG

…because the handler functions are not the same. Instead:
this.boundHandler = this.myHandler.bind(this);
Event.observe('myDiv', 'click', this.boundHandler);
// later...
Event.stopObserving('myDiv', 'click', this.boundHandler); // <= RIGHT, provided 'this' is right
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License