Table of contents
- 1.1. Summary
- 1.2. Syntax
- 1.3. Parameters
- 1.4. Compatibility
- 1.5. Example
- 2. Browser compatibility
- 2.1.1. Gecko notes
- 2.1.2. Opera notes
- 2.1.3. WebKit notes
- 2.2. Notes
- 2.3. Specification
Summary
removeEventListener() allows the removal of event listeners from the event target.
Syntax
element.removeEventListener(type, listener, useCapture)
Parameters
type- A string representing the event type being removed.
listener- The
listenerparameter indicates theEventListenerfunction to be removed. useCaptureOptional- Specifies whether the
EventListenerbeing removed was registered as a capturing listener or not. If a listener was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing listener does not affect a non-capturing version of the same listener, and vice versa.
useCapture became optional only in more recent versions of the major browsers; for example, it was not optional prior to Firefox 6. You should provide that parameter for broadest compatibility.Compatibility
addEventListener() and removeEventListener() are a recent addition to the standard; as such they may not be present in all browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing use of addEventListener() and removeEventListener() in implementations which do not natively support it.
if (!Element.prototype.addEventListener) {
var oListeners = {};
function runListeners(oEvent) {
if (!oEvent) { oEvent = window.event; }
for (var iLstId = 0, iElId = 0, oEvtListeners = oListeners[oEvent.type]; iElId < oEvtListeners.aEls.length; iElId++) {
if (oEvtListeners.aEls[iElId] === this) {
for (iLstId; iLstId < oEvtListeners.aEvts[iElId].length; iLstId++) { oEvtListeners.aEvts[iElId][iLstId].call(this, oEvent); }
break;
}
}
}
Element.prototype.addEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) {
if (oListeners.hasOwnProperty(sEventType)) {
var oEvtListeners = oListeners[sEventType];
for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) {
if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; }
}
if (nElIdx === -1) {
oEvtListeners.aEls.push(this);
oEvtListeners.aEvts.push([fListener]);
this["on" + sEventType] = runListeners;
} else {
var aElListeners = oEvtListeners.aEvts[nElIdx];
if (this["on" + sEventType] !== runListeners) {
aElListeners.splice(0);
this["on" + sEventType] = runListeners;
}
for (var iLstId = 0; iLstId < aElListeners.length; iLstId++) {
if (aElListeners[iLstId] === fListener) { return; }
}
aElListeners.push(fListener);
}
} else {
oListeners[sEventType] = { aEls: [this], aEvts: [ [fListener] ] };
this["on" + sEventType] = runListeners;
}
};
Element.prototype.removeEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) {
if (!oListeners.hasOwnProperty(sEventType)) { return; }
var oEvtListeners = oListeners[sEventType];
for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) {
if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; }
}
if (nElIdx === -1) { return; }
for (var iLstId = 0, aElListeners = oEvtListeners.aEvts[nElIdx]; iLstId < aElListeners.length; iLstId++) {
if (aElListeners[iLstId] === fListener) { aElListeners.splice(iLstId, 1); }
}
};
}
Example
This is an example of adding and then removing an event listener.
var div = document.getElementById('div');
var listener = function (event) {
/* do something here */
};
div.addEventListener('click', listener, false);
div.removeEventListener('click', listener, false);
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 1.0 | 1.0 (1.7 or earlier) | 9.0 | 7 | 1.0 |
useCapture made optional | (Yes) | 6.0 | 9.0 | (12.00) | (Yes) |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | 1.0 | 1.0 (1) | 9.0 | 6.0 | 1.0 |
Gecko notes
- Prior to Firefox 6, the browser would throw if the useCapture parameter was not explicitly false. Prior to Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6)
,
addEventListener()would throw an exception if the listener parameter wasnull; now the method returns without error, but without doing anything.
Opera notes
- Opera 12.00 will make the
useCaptureparameter optional (source)
WebKit notes
- Although WebKit has explicitly added [optional] to the
useCaptureparameter fairly recently, it had been working before the change. The new change landed in Safari 5.1 and Chrome 13.
Notes
If an EventListener
is removed from an EventTarget
while it is processing an event, it will not be triggered by the current actions. An EventListener
can never be invoked after being removed. Calling removeEventListener() with arguments which do not identify any currently registered EventListener
on the EventTarget has no effect. See also element.addEventListener()
.
Mozilla Developer Network