- Event
- Interfaces based on Event
- Constructor
- Instance properties
- Legacy and non-standard properties
- Instance methods
- Deprecated methods
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- JavaScript Events
- HTML Events
- Example
- Example
- Example
- Common HTML Events
- JavaScript Event Handlers
Event
The Event interface represents an event which takes place in the DOM.
An event can be triggered by the user action e.g. clicking the mouse button or tapping keyboard, or generated by APIs to represent the progress of an asynchronous task. It can also be triggered programmatically, such as by calling the HTMLElement.click() method of an element, or by defining the event, then sending it to a specified target using EventTarget.dispatchEvent() .
There are many types of events, some of which use other interfaces based on the main Event interface. Event itself contains the properties and methods which are common to all events.
Many DOM elements can be set up to accept (or «listen» for) these events, and execute code in response to process (or «handle») them. Event-handlers are usually connected (or «attached») to various HTML elements (such as , , , etc.) using EventTarget.addEventListener() , and this generally replaces using the old HTML event handler attributes. Further, when properly added, such handlers can also be disconnected if needed using removeEventListener() .
Note: One element can have several such handlers, even for the exact same event—particularly if separate, independent code modules attach them, each for its own independent purposes. (For example, a webpage with an advertising-module and statistics-module both monitoring video-watching.)
When there are many nested elements, each with its own handler(s), event processing can become very complicated—especially where a parent element receives the very same event as its child elements because «spatially» they overlap so the event technically occurs in both, and the processing order of such events depends on the Event bubbling and capture settings of each handler triggered.
Interfaces based on Event
Below is a list of interfaces which are based on the main Event interface, with links to their respective documentation in the MDN API reference.
Note that all event interfaces have names which end in «Event».
- AnimationEvent
- AudioProcessingEvent Deprecated
- BeforeUnloadEvent
- BlobEvent
- ClipboardEvent
- CloseEvent
- CompositionEvent
- CustomEvent
- DeviceMotionEvent
- DeviceOrientationEvent
- DragEvent
- ErrorEvent
- FetchEvent
- FocusEvent
- FontFaceSetLoadEvent
- FormDataEvent
- GamepadEvent
- HashChangeEvent
- HIDInputReportEvent
- IDBVersionChangeEvent
- InputEvent
- KeyboardEvent
- MediaStreamEvent Deprecated
- MessageEvent
- MouseEvent
- MutationEvent Deprecated
- OfflineAudioCompletionEvent
- PageTransitionEvent
- PaymentRequestUpdateEvent
- PointerEvent
- PopStateEvent
- ProgressEvent
- RTCDataChannelEvent
- RTCPeerConnectionIceEvent
- StorageEvent
- SubmitEvent
- SVGEvent Deprecated
- TimeEvent
- TouchEvent
- TrackEvent
- TransitionEvent
- UIEvent
- WebGLContextEvent
- WheelEvent
Constructor
Creates an Event object, returning it to the caller.
Instance properties
A boolean value indicating whether or not the event bubbles up through the DOM.
A boolean value indicating whether the event is cancelable.
A boolean indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM.
A reference to the currently registered target for the event. This is the object to which the event is currently slated to be sent. It’s possible this has been changed along the way through retargeting.
Indicates whether or not the call to event.preventDefault() canceled the event.
Indicates which phase of the event flow is being processed. It is one of the following numbers: NONE , CAPTURING_PHASE , AT_TARGET , BUBBLING_PHASE .
Indicates whether or not the event was initiated by the browser (after a user click, for instance) or by a script (using an event creation method, for example).
A reference to the object to which the event was originally dispatched.
The time at which the event was created (in milliseconds). By specification, this value is time since epoch—but in reality, browsers’ definitions vary. In addition, work is underway to change this to be a DOMHighResTimeStamp instead.
The name identifying the type of the event.
Legacy and non-standard properties
A historical alias to Event.stopPropagation() that should be used instead. Setting its value to true before returning from an event handler prevents propagation of the event.
The explicit original target of the event.
The original target of the event, before any retargetings.
A historical property still supported in order to ensure existing sites continue to work. Use Event.preventDefault() and Event.defaultPrevented instead.
A boolean value indicating whether the given event will bubble across through the shadow root into the standard DOM. Use composed instead.
Instance methods
Returns the event’s path (an array of objects on which listeners will be invoked). This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.
Cancels the event (if it is cancelable).
For this particular event, prevent all other listeners from being called. This includes listeners attached to the same element as well as those attached to elements that will be traversed later (during the capture phase, for instance).
Stops the propagation of events further along in the DOM.
Deprecated methods
Initializes the value of an Event created. If the event has already been dispatched, this method does nothing. Use the constructor ( Event() instead).
Specifications
Browser compatibility
BCD tables only load in the browser
See also
- Types of events available: Event reference
- Comparison of Event Targets ( target vs. currentTarget vs. relatedTarget vs. originalTarget )
- Creating and triggering custom events
Found a content problem with this page?
This page was last modified on Jul 3, 2023 by MDN contributors.
Your blueprint for a better internet.
JavaScript Events
HTML events are «things» that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can «react» on these events.
HTML Events
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
- An HTML web page has finished loading
- An HTML input field was changed
- An HTML button was clicked
Often, when events happen, you may want to do something.
JavaScript lets you execute code when events are detected.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
In the following example, an onclick attribute (with code), is added to a element:
Example
In the example above, the JavaScript code changes the content of the element with >
In the next example, the code changes the content of its own element (using this.innerHTML ):
Example
JavaScript code is often several lines long. It is more common to see event attributes calling functions:
Example
Common HTML Events
Here is a list of some common HTML events:
Event | Description |
---|---|
onchange | An HTML element has been changed |
onclick | The user clicks an HTML element |
onmouseover | The user moves the mouse over an HTML element |
onmouseout | The user moves the mouse away from an HTML element |
onkeydown | The user pushes a keyboard key |
onload | The browser has finished loading the page |
JavaScript Event Handlers
Event handlers can be used to handle and verify user input, user actions, and browser actions:
- Things that should be done every time a page loads
- Things that should be done when the page is closed
- Action that should be performed when a user clicks a button
- Content that should be verified when a user inputs data
- And more .
Many different methods can be used to let JavaScript work with events:
- HTML event attributes can execute JavaScript code directly
- HTML event attributes can call JavaScript functions
- You can assign your own event handler functions to HTML elements
- You can prevent events from being sent or being handled
- And more .
You will learn a lot more about events and event handlers in the HTML DOM chapters.