DEV Community

Discussion on: removeEventListener not working.

Collapse
 
merri profile image
Vesa Piittinen

My guess of what you want is this:

const header = document.querySelector(`header h1`);
const button = document.querySelector(`header button`);

function addText() {
   header.insertAdjacentText(`afterend`, `Yeah`);
   console.log(`yeah`);
   button.removeEventListener(`click`, addText);
}

button.addEventListener(`click`, addText);
Enter fullscreen mode Exit fullscreen mode

Add listener to button, upon clicking remove the listener from the button.