Javascript examples for DOM:Element removeEventListener
The removeEventListener() method removes an event handler attached with the addEventListener() method.
Parameter | Description |
---|---|
event | Required. event name. |
function | Required. function to remove. |
useCapture | Optional. |
Possible values for useCapture:
If the event handler was attached twice, one with capturing and one bubbling, each must be removed separately.
No return value
The following code shows how to Remove a "mousemove" event that has been attached with the addEventListener() method:
<!DOCTYPE html> <html> <head> <style> #myDIV {/*from w ww .ja v a2 s .com*/ background-color: coral; border: 1px solid; padding: 50px; color: white; } </style> </head> <body> <div id="myDIV">mouse your mouse here. <p>Click the button to remove the DIV's event handler.</p> <button onclick="removeHandler()" id="myBtn">Test</button> </div> <p id="demo"></p> <script> document.getElementById("myDIV").addEventListener("mousemove", myFunction); function myFunction() { document.getElementById("demo").innerHTML = Math.random(); } function removeHandler() { document.getElementById("myDIV").removeEventListener("mousemove", myFunction); } </script> </body> </html>