Javascript examples for DOM:Element removeEventListener
Element removeEventListener() Method - If browsers do not support removeEventListener() method, use the detachEvent() method.
<!DOCTYPE html> <html> <head> <style> #myDIV {/* w w w. j a v a 2s .c o m*/ background-color: coral; border: 1px solid; padding: 50px; color: white; } </style> </head> <body> <div id="myDIV">move 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> var x = document.getElementById("myDIV"); if (x.addEventListener) { x.addEventListener("mousemove", myFunction); } else if (x.attachEvent) { x.attachEvent("onmousemove", myFunction); } function myFunction() { document.getElementById("demo").innerHTML = Math.random(); } function removeHandler() { if (x.removeEventListener) { x.removeEventListener("mousemove", myFunction); } else if (x.detachEvent) { x.detachEvent("onmousemove", myFunction); } } </script> </body> </html>