Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM removeEventListener() method

HTML DOM removeEventListener() method

Document Object Reference Document Object

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.

 可能值:     
  • true - The event handler is removed during the capturing phase
  • false - Default. The event handler is removed during the bubbling phase
  • **Note:** If you add an event handler twice, once in the capturing phase and once in the bubbling phase, you must remove the event separately. |

    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 Reference Document Object