Table of Contents

<HTMLElement>.once()

Attaches a callback function for a named event to any HTMLElement. The callback will be automatically removed after it has been called.

Usage:
HTMLElement = Element.once(name, callback)
Member of:
HTMLElement
Parameters:
  1. name – the name of the event to attach to (required).
    Examples: 'click', 'hover', etc.
  2. callback – the function to call when the event occurs (required).
Returns:
The HTMLElement it was called on, thus allowing for command chaining.
Notes
  • This is functionally identical to the .on method and uses similar code. See there for more information and more examples. .

Example

let foo document.getElementById('foo');
foo.once('click', function(e) {
  console.log(e);
});

See also

More information