Element addEventListener() Method - Use removeEventListener() method to remove an event handler attached with the addEventListener() method: - Javascript DOM

Javascript examples for DOM:Element addEventListener

Description

Element addEventListener() Method - Use removeEventListener() method to remove an event handler attached with the addEventListener() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*from  ww w . j a v a2 s.  c o m*/
    background-color: coral;
    border: 1px solid;
    padding: 50px;
    color: white;
}
</style>
</head>
<body>

<div id="myDIV">move 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>

Related Tutorials