5

I want to know if it is possible to remove an event listener without a reference to the event handler function that should be removed.

Jquery's $().unbind('touchmove') will not work because the event listener was added using Javascript's addEventListener('touchmove', handlerFunction), not Jquery's bind function.

removeEventListener('touchmove') does not seem to work, which is consistent with the Javascript function spec.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
laker
  • 569
  • 1
  • 4
  • 17

2 Answers2

0

You can get all event listeners for a DOM element and remove one/all:

var eventlistener = getEventListeners(window)["DOMContentLoaded"][index];
window.removeEventListener("DOMContentLoaded", eventlistener.listener, eventlistener.useCapture);

Taken from this answer: https://stackoverflow.com/a/26845805/712700

goamn
  • 1,939
  • 2
  • 23
  • 39
-2

You should always add events to a specific object so you can access them later, try this.

to add:

window.addEventHandler( 'touchmove', function );

to remove:

window.removeEventListener( 'touchmove', function );
Joel
  • 1,309
  • 2
  • 10
  • 20