guide
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 the Event 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 in Event > Interfaces based on Event.
This topic provides an index to the main sorts of events you might be interested in (animation, clipboard, workers etc.) along with the main classes that implement those sorts of events.
Event index
| Event type | Description | Documentation |
|---|---|---|
| Animation |
Events related to the Web Animation API. Used to respond to changes in animation status (e.g., when an animation starts or ends). |
Animation events fired on
Document,
Window,
HTMLElement.
|
| Asynchronous data fetching | Events related to the fetching data. |
Events fired on
AbortSignal,
XMLHttpRequest,
FileReader.
|
| Clipboard |
Events related to the Clipboard API. Used to notify when content is cut, copied, or pasted. |
Events fired on
Document,
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 on
Element.
|
| CSS transition |
Events related to CSS Transitions. Provides notification events when CSS transitions start, stop, are cancelled, etc. |
Events fired on
Document,
HTMLElement,
Window.
|
| Database |
Events related to database operations: opening, closing, transactions, errors, etc. |
Events fired on
IDBDatabase,
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 the HTML Drag and Drop API and wheel 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 on
Element,
Window.
|
| Form |
Events related to forms being constructed, reset and submitted. |
Events fired on
HTMLFormElement.
|
| Fullscreen |
Events related to the Fullscreen API. Used to notify when the transitioning between full screen and windowed modes, and also of errors occurring during this transition. |
Events fired on
Document,
Element.
|
| Gamepad |
Events related to the Gamepad API. |
Events fired on
Window.
|
| 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 the History API. |
Events fired on
Window.
|
| HTML element content display management |
Events related to changing the state of a display or textual element. |
Events fired on
HTMLDetailsElement,
HTMLDialogElement,
HTMLSlotElement.
|
| Inputs |
Events related to HTML input elements e.g. [``](/firefox/mdn/html/reference/elements/input/), [` In addition to the events fired by built-in interfaces, you can create and dispatch DOM events yourself. Such events are commonly called synthetic events, as opposed to the events fired by the browser. Creating custom eventsEvents can be created with the
This code example uses the EventTarget.dispatchEvent() method. Adding custom data – CustomEvent()To add more data to the event object, the CustomEvent interface exists and the detail property can be used to pass custom data. For example, the event could be created as follows:
This will then allow you to access the additional data in the event listener:
Adding custom data – subclassing EventThe
This code example defines a The event could then be created as follows:
The additional data can then be accessed in the event listeners using the custom properties:
Event bubblingIt 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:
Creating and dispatching events dynamicallyElements can listen for events that haven’t been created yet:
Triggering built-in eventsThis example demonstrates simulating a click (that is programmatically generating a click event) on a checkbox using DOM methods. View the example in action.
Registering event handlersThere 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 corresponding onevent property or by registering the handler as a listener for the element using the
Using onevent propertiesBy 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 a
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 the EventTarget.addEventListenerThe most flexible way to set an event handler on an element is to use the
The following example shows how a
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 the Using AbortSignalA 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 same
This event handler can then be removed like this:
Interaction of multiple event handlersThe When an event is dispatched, listeners are called in phases. There are two phases: capture and bubble. 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 specifying Calling SpecificationsSpecificationsStandards references are available on the canonical MDN page.
See also |