== .once ==

.once()

Attaches a callback function for a named event to any HTMLElement. The callback will be automatically removed after it has been called.
Usage:
[[mdn>Web/API/HTMLElement|HTMLElement]] = Element.''once''(name, callback)
Member of:
[[mdn>Web/API/HTMLElement|HTMLElement]]
Parameters:
- name – the name of the event to attach to (required). \\ Examples: ''%%'click'%%'', ''%%'hover'%%'', etc. - callback – the function to call when the event occurs (required).
Returns:
The [[mdn>Web/API/HTMLElement|HTMLElement]] it was called on, thus allowing for command chaining.
Notes
* This is functionally identical to the [[toolbox:event:on:index|.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 ===== * [[toolbox:event:on:index|.on]] * [[toolbox:event:off:index|.off]] ===== More information ===== * [[mdn>API/EventTarget/addEventListener|EventTarget: addEventListener() method]] on MDN.