- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript removeEventListener
JavaScript Events
JavaScript removeEventListener
removeEventListenerdetaches a handler, but only if it can identify it.
You have to pass the same function reference you added.
This is why anonymous handlers can never be removed.
Example
Example
javascript
document.body.innerHTML = '<button id="btn">Go</button>';
const btn = document.getElementById("btn");
let count = 0;
function bump() {
count = count + 1;
}
btn.addEventListener("click", bump);
btn.click();
btn.removeEventListener("click", bump);
btn.click();
console.log(count);