Skip to content

ElementWrapper.once()

Definition

ts
once<K extends keyof HTMLElementEventMap>(
    event: K,
    callback: EventCallback<T, HTMLElementEventMap[K]>,
): this;
once(event: string, callback: EventCallback<T>): this;

Description

Adds an event listener to all selected elements that fires once, then automatically removes itself.

The callback function is of type EventCallback.

Useful for one-time interactions such as initialization on first click, or confirming a destructive action.

Example

html
<button class="start-btn">Start</button>
ts
$('.start-btn').once('click', () => {
  console.log('Started! This will not fire again.');
});