DOM events
Events are fired to notify code of "interesting changes" that may affect code execution. These can arise from user interactions such as using a mouse or resizing a window, changes in the state of the underlying environment (e.g., low battery or media events from the operating system), and other causes.
Each event is represented by an object that is based on theEvent
interface, and may have additional custom fields and/or functions to provide information about what happened. The documentation for every event has a table (near the top) that includes a link to the associated event interface, and other relevant information. A full list of the different event types is given inEvent > Interfaces based on Event.
This topic provides an index to the mainsorts of events you might be interested in (animation, clipboard, workers etc.) along with the main classes that implement those sorts of events.
In this article
Event index
Event type | Description | Documentation |
---|---|---|
Animation | Events related to theWeb Animation API. Used to respond to changes in animation status (e.g., when an animation starts or ends). | Animation events fired onDocument ,Window ,HTMLElement . |
Asynchronous data fetching | Events related to the fetching data. | Events fired onAbortSignal ,XMLHttpRequest ,FileReader . |
Clipboard | Events related to theClipboard API. Used to notify when content is cut, copied, or pasted. | Events fired onDocument ,Element ,Window . |
Composition | Events related to composition; entering text "indirectly" (rather than using normal keyboard presses). For example, text entered via a speech to text engine, or using special key combinations that modify keyboard presses to represent new characters in another language. | Events fired onElement . |
CSS transition | Events related toCSS Transitions. Provides notification events when CSS transitions start, stop, are cancelled, etc. | Events fired onDocument ,HTMLElement ,Window . |
Database | Events related to database operations: opening, closing, transactions, errors, etc. | Events fired onIDBDatabase ,IDBOpenDBRequest ,IDBRequest ,IDBTransaction . |
DOM mutation | Events related to modifications to the Document Object Model (DOM) hierarchy and nodes. | Warning:Mutation Events are deprecated.Mutation Observers should be used instead. |
Drag'n'drop, Wheel | Events related to using theHTML Drag and Drop API andwheel events. Drag and Wheel events are derived from mouse events. While they are fired when using mouse wheel or drag/drop, they may also be used with other appropriate hardware. | Drag events fired on Wheel events fired on |
Focus | Events related to elements gaining and losing focus. | Events fired onElement ,Window . |
Form | Events related to forms being constructed, reset and submitted. | Events fired onHTMLFormElement . |
Fullscreen | Events related to theFullscreen API. Used to notify when the transitioning between full screen and windowed modes, and also of errors occurring during this transition. | Events fired onDocument ,Element . |
Gamepad | Events related to theGamepad API. | Events fired onWindow . |
Gestures | Touch events are recommended for implementing gestures. | Events fired on In addition there are a number of non-standard gesture events:
|
History | Events related to theHistory API. | Events fired onWindow . |
HTML element content display management | Events related to changing the state of a display or textual element. | Events fired onHTMLDetailsElement ,HTMLDialogElement ,HTMLSlotElement . |
Inputs | Events related to HTML input elements e.g. | Events fired onHTMLElement ,HTMLInputElement . |
Keyboard | Events related to using akeyboard. Used to notify when keys are moved up, down, or just pressed. | Events fired onDocument ,Element . |
Loading/unloading documents | Events related to loading and unloading documents. | |
Manifests | Events related to installation ofprogressive web app manifests. | Events fired onWindow . |
Media | Events related to media usage (including theMedia Capture and Streams API,Web Audio API,Picture-in-Picture API, etc.). | Events fired onScriptProcessorNode ,HTMLMediaElement ,AudioTrackList ,AudioScheduledSourceNode ,MediaRecorder ,MediaStream ,MediaStreamTrack ,VideoTrackList ,HTMLTrackElement ,OfflineAudioContext ,TextTrack ,TextTrackList ,Element/audio,Element/video. |
Messaging | Events related to a window receiving a message from another browsing context. | Events fired onWindow . |
Mouse | Events related to using acomputer mouse. Used to notify when the mouse is clicked, double-clicked, up and down events, right-click, movement in and out of an element, text selection, etc. Pointer events provide a hardware-agnostic alternative to mouse events. Drag and Wheel events are derived from mouse events. | Mouse events fired onElement |
Network/Connection | Events related to gaining and losing network connection. | Events fired on Events fired on |
Payments | Events related to thePayment Request API. | Events fired on |
Performance | Events related to any performance-related spec grouped intoPerformance APIs. | Events fired on |
Pointer | Events related to thePointer Events API. Provides hardware-agnostic notification from pointing devices including Mouse, Touch, pen/stylus. | Events fired onDocument ,HTMLElement . |
Events related to printing. | Events fired onWindow . | |
Promise rejection | Events sent to the global script context when any JavaScript promise is rejected. | Events fired onWindow . |
Sockets | Events related to theWebSockets API. | Events fired onWebSocket . |
SVG | Events related to SVG images. | Events fired on |
Text selection | Selection API events related to selecting text. | Event ( |
Touch | Events related to theTouch Events API. Provides notification events from interacting with a touch sensitive screen (i.e., using a finger or stylus). Not related to theForce Touch API. | Events fired onDocument ,Element . |
Virtual reality | Events related to theWebXR Device API. Warning: TheWebVR API (and associated | Events fired onXRSystem ,XRSession ,XRReferenceSpace . |
RTC (real time communication) | Events related to theWebRTC API. | Events fired onRTCDataChannel ,RTCDTMFSender ,RTCIceTransport ,RTCPeerConnection . |
Server-sent events | Events related to theserver sent events API. | Events fired onEventSource . |
Speech | Events related to theWeb Speech API. | Events fired onSpeechSynthesisUtterance . |
Workers | Events related to theWeb Workers API,Service Worker API,Broadcast Channel API, andChannel Messaging API. Used to respond to new messages and message sending errors. Service workers can also be notified of other events, including push notifications, users clicking on displayed notifications, that push subscription has been invalidated, deletion of items from the content index, etc. | Events fired onServiceWorkerGlobalScope ,DedicatedWorkerGlobalScope ,SharedWorkerGlobalScope ,WorkerGlobalScope ,Worker ,BroadcastChannel ,MessagePort . |
Creating and dispatching events
In addition to the events fired by built-in interfaces, you can create and dispatch DOM events yourself. Such events are commonly calledsynthetic events, as opposed to the events fired by the browser.
Creating custom events
Events can be created with theEvent
constructor as follows:
const event = new Event("build");// Listen for the event.elem.addEventListener("build", (e) => { /* … */});// Dispatch the event.elem.dispatchEvent(event);
This code example uses theEventTarget.dispatchEvent() method.
Adding custom data – CustomEvent()
To add more data to the event object, theCustomEvent interface exists and thedetail property can be used to pass custom data.For example, the event could be created as follows:
const event = new CustomEvent("build", { detail: elem.dataset.time });
This will then allow you to access the additional data in the event listener:
function eventHandler(e) { console.log(`The time is: ${e.detail}`);}
Adding custom data – subclassing Event
TheEvent
interface can also be subclassed. This is particularly useful for reuse, or for more complex custom data, or even adding methods to the event.
class BuildEvent extends Event { #buildTime; constructor(buildTime) { super("build"); this.#buildTime = buildTime; } get buildTime() { return this.#buildTime; }}
This code example defines aBuildEvent
class with a read-only property and a fixed event type.
The event could then be created as follows:
const event = new BuildEvent(elem.dataset.time);
The additional data can then be accessed in the event listeners using the custom properties:
function eventHandler(e) { console.log(`The time is: ${e.buildTime}`);}
Event bubbling
It is often desirable to trigger an event from a child element and have an ancestor catch it; optionally, you can include data with the event:
<form> <textarea></textarea></form>
const form = document.querySelector("form");const textarea = document.querySelector("textarea");// Create a new event, allow bubbling, and provide any data you want to pass to the "detail" propertyconst eventAwesome = new CustomEvent("awesome", { bubbles: true, detail: { text: () => textarea.value },});// The form element listens for the custom "awesome" event and then consoles the output of the passed text() methodform.addEventListener("awesome", (e) => console.log(e.detail.text()));// As the user types, the textarea inside the form dispatches/triggers the event to fire, using itself as the starting pointtextarea.addEventListener("input", (e) => e.target.dispatchEvent(eventAwesome));
Creating and dispatching events dynamically
Elements can listen for events that haven't been created yet:
<form> <textarea></textarea></form>
const form = document.querySelector("form");const textarea = document.querySelector("textarea");form.addEventListener("awesome", (e) => console.log(e.detail.text()));textarea.addEventListener("input", function () { // Create and dispatch/trigger an event on the fly // Note: Optionally, we've also leveraged the "function expression" (instead of the "arrow function expression") so "this" will represent the element this.dispatchEvent( new CustomEvent("awesome", { bubbles: true, detail: { text: () => textarea.value }, }), );});
Triggering built-in events
This example demonstrates simulating a click (that is programmatically generating a click event) on a checkbox using DOM methods.View the example in action.
function simulateClick() { const event = new MouseEvent("click", { view: window, bubbles: true, cancelable: true, }); const cb = document.getElementById("checkbox"); const cancelled = !cb.dispatchEvent(event); if (cancelled) { // A handler called preventDefault. alert("cancelled"); } else { // None of the handlers called preventDefault. alert("not cancelled"); }}
Registering event handlers
There are two recommended approaches for registering handlers. Event handler code can be made to run when an event is triggered either by assigning it to the target element's correspondingonevent property or by registering the handler as a listener for the element using theaddEventListener()
method. In either case, the handler will receive an object that conforms to theEvent
interface (or aderived interface). The main difference is that multiple event handlers can be added (or removed) using the event listener methods.
Warning:A third approach for setting event handlers using HTML onevent attributes is not recommended! They inflate the markup and make it less readable and harder to debug. For more information, seeInline event handlers.
Using onevent properties
By convention, JavaScript objects that fire events have corresponding "onevent" properties (named by prefixing "on" to the name of the event). These properties are called to run associated handler code when the event is fired, and may also be called directly by your own code.
To set event handler code, you can just assign it to the appropriate onevent property. Only one event handler can be assigned for every event in an element. If needed, the handler can be replaced by assigning another function to the same property.
The following example shows how to set agreet()
function for theclick
event using theonclick
property.
const btn = document.querySelector("button");function greet(event) { console.log("greet:", event);}btn.onclick = greet;
Note that an object representing the event is passed as the first argument to the event handler. This event object either implements or is derived from theEvent
interface.
EventTarget.addEventListener
The most flexible way to set an event handler on an element is to use theEventTarget.addEventListener
method. This approach allows multiple listeners to be assigned to an element and enables listeners to beremoved, if needed, usingEventTarget.removeEventListener
.
Note:The ability to add and remove event handlers allows you to, for example, have the same button performing different actions in different circumstances. In addition, in more complex programs, cleaning up old/unused event handlers can improve efficiency.
The following example shows how agreet()
function can be set as a listener/event handler for theclick
event (you could use an anonymous function expression instead of a named function if desired). Note again that the event is passed as the first argument to the event handler.
const btn = document.querySelector("button");function greet(event) { console.log("greet:", event);}btn.addEventListener("click", greet);
The method can also take additional arguments/options to control aspects of how the events are captured and removed. More information can be found on theEventTarget.addEventListener
reference page.
Using AbortSignal
A notable event listener feature is the ability to use an abort signal to clean up multiple event handlers at the same time.
This is done by passing the sameAbortSignal
to theaddEventListener()
call for all the event handlers that you want to be able to remove together. You can then callabort()
on the controller owning theAbortSignal
, and it will remove all event handlers that were added with that signal. For example, to add an event handler that we can remove with anAbortSignal
:
const controller = new AbortController();btn.addEventListener( "click", (event) => { console.log("greet:", event); }, { signal: controller.signal },); // pass an AbortSignal to this handler
This event handler can then be removed like this:
controller.abort(); // removes any/all event handlers associated with this controller
Interaction of multiple event handlers
Theonevent
IDL property (for example,element.onclick = ...
) and the HTMLonevent
content attribute (for example,<button>
) both target the same single handler slot. HTML loads before JavaScript could access the same element, so usually JavaScript replaces what's specified in HTML. Handlers added withaddEventListener()
are independent. Usingonevent
does not remove or replace listeners added withaddEventListener()
, and vice versa.
When an event is dispatched, listeners are called in phases. There are two phases:capture andbubble. In the capture phase, the event starts from the highest ancestor element and moves down the DOM tree until it reaches the target. In the bubble phase, the event moves in the opposite direction. Event listeners by default listen in the bubble phase, and they can listen in the capturing phase by specifyingcapture: true
withaddEventListener()
. Within a phase, listeners run in the order they were registered. Theonevent
handler is registered the first time it becomes non-null; later reassignments change only its callback, not its position in the order.
CallingEvent.stopPropagation()
prevents calling listeners on other elements later in the propagation chain.Event.stopImmediatePropagation()
also prevents calling remaining listeners on the same element.
Specifications
Specification |
---|
DOM> # events> |
HTML> # events-2> |