HTML DOM removeEventListener()Method

Document 对象参考手册Document Object

Example

Remove the "mousemove" event added by the addEventListener() method:

// Add event handler to the document
document.addEventListener("mousemove", myFunction);

// Remove event handler from the document
document.removeEventListener("mousemove", myFunction);

Try it yourself »

Definition and Usage

The document.removeEventListener() method is used to removedocument.addEventListener()the event handler added by the method.

Note:To remove an event handler, the function passed to addEventListener() must be an external function, as shown in the example above (myFunction).

Anonymous functions, like "document.removeEventListener("event", function(){ myScript});" the event cannot be removed.

Tip:Use the element.addEventListener()andelement.removeEventListener()method to add or remove event handlers for the specified element.


Browser Support

The numbers in the table specify the first browser version that supports the method.

Method
removeEventListener() 1.0 9.0 1.0 1.0 7.0

Note:Internet Explorer 8 and earlier IE versions do not support the removeEventListener() method, and Opera 7.0 and earlier Opera versions do not support it either. However, for these browsers that do not support this function, you can use thedetachEvent()method to remove the event handler added by the attachEvent() method (see "More Examples" for a cross-browser solution).


Syntax

document.removeEventListener(event, function, useCapture)

Parameter Values

Parameter Description
event Required. The name of the event to remove.

Note:Do not use the "on" prefix. For example, use "click" instead of "onclick".

Tip:For all HTML DOM events, see our completeHTML DOM Event Object Reference。
function Required. Specifies the function to remove.
useCapture Optional. A Boolean value that specifies the phase in which to remove the event handler.

Possible values:
  • true - The event handler is removed in the capturing phase
  • false - Default. The event handler is removed in the bubbling phase
Note:If you add the event handler twice, once in the capturing phase and once in the bubbling phase, you must remove each one separately.

Technical Details

DOM Version: DOM Level 2 events
Return Value: No return value
Change Log: In Firefox 6 and Opera 12.0, the useCapture parameter is optional. (It has always been optional in Chrome, IE, and Safari).


More Examples

Example

If the browser does not support the removeEventListener() method, you can use the detachEvent() method instead.

This example demonstrates a cross-browser solution:

if (document.removeEventListener) { // All browsers, except IE 8 and earlier
    document.removeEventListener("mousemove", myFunction);
} else if (document.detachEvent) { // IE 8 and earlier
    document.detachEvent("onmousemove", myFunction);
}

Try it yourself »


Related Articles

JavaScript Tutorial:HTML DOM EventListener

JavaScript Reference:element.removeEventListener()


Document 对象参考手册Document Object

Other Extensions