Element removeEventListener() Method - If browsers do not support removeEventListener() method, use the detachEvent() method. - Javascript DOM

Javascript examples for DOM:Element removeEventListener

Description

Element removeEventListener() Method - If browsers do not support removeEventListener() method, use the detachEvent() method.

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials