Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

EventTarget.addEventListener — registering event listeners

EventTarget.addEventListener registers an event callback. The capture, once, and passive options control invocation and whether the listener can cancel the event.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

target.addEventListener(type, callback, options);
// options: boolean capture, or { capture, once, passive, signal }

EventTarget.addEventListener registers a callback for event type type. The third argument is an options object or a boolean representing capture. This API belongs to the DOM standard, not ECMAScript itself.

capture: true selects capturing rather than bubbling on ancestors; either setting permits invocation at the target itself. Both capture and once default to false. With once: true, the registration is removed before the callback is invoked. If the callback dispatches an event during that invocation, the nested dispatch cannot invoke it again through the same registration. passive: true prevents cancellation through preventDefault(), but does not stop propagation. To cancel a cancelable event, explicitly set passive: false: omission does not always mean false.

Duplicates are identified by type, callback identity, and capture; registering again does not update options. removeEventListener requires those same three values. The example uses a standalone target, so it does not demonstrate propagation between elements.

Examples

const target = new EventTarget();
let calls = 0;
target.addEventListener('demo', (event) => {
  calls++;
  event.preventDefault();
}, { capture: false, once: true, passive: false });

const first = new Event('demo', { cancelable: true });
target.dispatchEvent(first);
target.dispatchEvent(new Event('demo'));
console.log(calls, first.defaultPrevented);

Result

1 true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources