DOM EventTarget.addEventListener() Method
The addEventListener() method runs a function whenever a given event, such as a click or key press, happens on an element, the document or the window.
Description
The addEventListener() method of the EventTarget interface sets up a function that is called whenever the specified event is delivered to the target. Targets include elements, the document, the window and other objects that support events.
Unlike onclick and similar properties, you can add several listeners for the same event, and they run in the order they were added. The options object controls how the listener works: once: true removes it after the first call, passive: true promises not to call preventDefault() (which helps scrolling performance), and signal lets you remove it with an AbortController.
To remove a listener, call removeEventListener() with the same type and the same function reference, so avoid anonymous functions if you need to remove it later. Adding one listener to a parent and checking event.target, known as event delegation, is often simpler than adding listeners to many children.
Syntax
target.addEventListener(type, listener)
target.addEventListener(type, listener, options)Parameters
- typeThe name of the event to listen for, such as
"click"or"input". - listenerThe function to call when the event happens. It receives an
Eventobject. - optionsOptional. An object with
once,passive,captureandsignalsettings, ortrueto use the capture phase.
Return Value
None (undefined).
Example
Browser Support
The following information will show you the current browser support for the EventTarget.addEventListener() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





