About Lesson
Removing Event Listeners
You can remove an event listener using the removeEventListener() method. However, this works only if you reference the same function as the one used to add the listener.
Example:
JavaScript
function handleClick() {
alert("Button clicked!");
}
let button = document.querySelector("#removeButton");
button.addEventListener("click", handleClick);
// Removing the event listener
button.removeEventListener("click", handleClick);
If the user clicks the button after removeEventListener() is called, the alert won’t appear since the listener has been removed.