HTML DOM removeEventListener() method
Example
Remove the "mousemove" event added by the addEventListener() method:
// Add an event handler to the document
document.addEventListener("mousemove", myFunction);
// Remove the event handler from the document
document.removeEventListener("mousemove", myFunction);
Try it »
Definition and usage
The document.removeEventListener() method is used to remove the event handler added by the document.addEventListener() method.
Note: To remove an event handler, the execution function of addEventListener() must use an external function, as shown in the example above (myFunction).
Anonymous functions, like "document.removeEventListener("event", function(){ myScript });" cannot be removed.
Tip: Use * element*.addEventListener() and element.removeEventListener() method to add or remove an event handler for a specified element.
Browser support
The numbers in the table indicate the first browser version that supports this 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, nor do Opera 7.0 and earlier versions of Opera. However, for these browsers that do not support this function, you can use the detachEvent() method to remove the event handler added by the attachEvent() method (see "More Examples" for the cross-browser solution).
Syntax
document.removeEventListener(
event
,
function
,
useCapture
)
Parameter value
| Parameters | Description |
|---|---|
| event | Required. The name of the event to be removed. |
**注意:** 不要使用 "on" 前缀。 例如,使用 "click" ,而不是使用 "onclick"。
Hint: For all HTML DOM events, you can view our complete [HTML DOM Event Object Reference Manual](/jsref/dom_obj_event.asp). |
| function | Required. The function to be removed. | | useCapture | Optional. A Boolean value specifying the phase in which to remove the event handler.
可能值:
Technical Details
| DOM version: | DOM Level 2 events |
|---|---|
| Return value: | No return value |
| Modification record: | 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 the cross-browser solution:
if (document.removeEventListener) { // All browsers, except IE 8 and earlier IE versions
document.removeEventListener("mousemove", myFunction);
} else if (document.detachEvent) { // IE 8 and earlier IE versions
document.detachEvent("onmousemove", myFunction);
}
Try it »
Related Articles
JavaScript tutorial: HTML DOM EventListener
JavaScript Reference Manual: element.removeEventListener()
Document Object