4 ms·
My issue is how to remove the event listener once you've done document.addEventListener('click', handleEvent.bind(this)); Doing document.removeEventL
by cocoflunchy 13y ago
My issue is how to remove the event listener once you've done
document.addEventListener('click', handleEvent.bind(this));
Doing
document.removeEventListener('click', handleEvent);
doesn't work, so how can you get rid of it?
- Vekz 13y agoYou would need a reference to the function that you used bind on before binding it to the event var eventHandler = handleEvent.bind(this); document.addEventListener('click', eventHandler); document.removeEventListener('click', eventHandler); May seem redundant but if you are trying to remove a listener without using bind you still need a reference.
- timothya 13y agoYou could do: var boundEvent = handleEvent.bind(this); document.addEventListener('click', boundEvent); and later: document.removeEventListener('click', boundEvent); This situation is a bit messier, though, because you have to keep track of both `handleEvent` and `boundEvent`.
- cocoflunchy 13y agoNow it seems obvious... thank you ;)
- cjfont 13y agoSince bind() creates a new function just before it's added as a listener, you'll have to save that function somewhere and use that as the listener you want to remove.