HTML DOM removeEventListener() method
Example
Remove the "mousemove" event added by the addEventListener() method:
// Add
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
// Remove
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
Try it »
Definition and Usage
The removeEventListener() method is used to remove the event handler added by the 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.
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
element
.removeEventListener(
event
,
function
,
useCapture
)
Parameter value
| Parameter | 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:
var x = document.getElementById("myDIV");
if (x.removeEventListener) { // // All browsers, except IE 8 and earlier IE versions
x.removeEventListener("mousemove", myFunction);
} else if (x.detachEvent) { // IE 8 and earlier IE versions
x.detachEvent("onmousemove", myFunction);
}
Try it »
Related pages
JavaScript tutorial: HTML DOM EventListener
JavaScript Reference Manual: document.removeEventListener()
Element object