logo
logo

Stop Listening for Click

Stop Listening for Click

Create a function called
stopListeningForClick(element, listenerToRemove)
that takes in two arguments: an HTML element and a function representing the click event listener to remove. The function should remove the specified click event listener from the element using the
removeEventListener()
method.
For example, if
stopListeningForClick()
is called with an element that has a click listener function
handleClick
, the function should remove that listener from the element so that it no longer responds to click events.
Note that the click listener function should match the same function that was originally added as a listener using
addEventListener()
.
// Given the following HTML:
// <button id="my-button">Click me!</button>

const myButton = document.querySelector('#my-button');
const handleClick = () => {
  console.log('Button clicked!');
};
myButton.addEventListener('click', handleClick);
// ...later, when you want to remove the click listener
stopListeningForClick(myButton, handleClick);

Console
Submit
Solution
00:00