removeEventListener method

The removeEventListener method allows you to remove an event handler previously assigned via addEventListener. To do this, in the parameters you need to pass the event type and the same function that were passed when binding the event.

Syntax

javascriptelement.removeEventListener('event type', function);

Example

Let's make it so that clicking on an element triggers only once:

html<input type="button" id="button" value="click me">
javascriptlet button = document.querySelector('#button');
button.addEventListener('click', func); // we assign the event

function func() {
  alert('!!!');
  button.removeEventListener('click', func); // remove it after clicking
};

Результат:

See also