The document.removeEventListener()
method removes an event handler
that has been added with the document.addEventListener() method.
removeEventListener |
Yes | 9.0 | Yes | Yes | Yes |
document.removeEventListener(event, function, useCapture)
Parameter | Description |
---|---|
event | Required. name of the event to remove. |
function | Required. Specifies the function to remove. |
useCapture | Optional.
|
No return value
The following code shows how to remove a "mousemove" event that has been attached with the addEventListener() method.
<!DOCTYPE html>
<html>
<body>
<button onclick="removeHandler()">test</button>
<p id="demo"></p>
<script>
document.addEventListener("mousemove", myFunction);
<!-- w ww . j ava2 s . c om-->
function myFunction() {
document.getElementById("demo").innerHTML = Math.random();
}
function removeHandler() {
document.removeEventListener("mousemove", myFunction);
}
</script>
</body>
</html>
The code above is rendered as follows: