1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
---
title: Event
slug: Web/API/Event
tags:
- API
- Constructor
- DOM
- Event
- Interface
- NeedsTranslation
- Reference
- TopicStub
- UI
- listener
translation_of: Web/API/Event
---
<p>{{APIRef("DOM")}}</p>
<p><span class="seoSummary">The <code><strong>Event</strong></code> interface represents an event which takes place in the DOM.</span></p>
<p>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 <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click" title="The HTMLElement.click() method simulates a mouse click on an element."><code>HTMLElement.click()</code></a> method of an element, or by defining the event, then sending it to a specified target using <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent" title="Dispatches an Event at the specified EventTarget, (synchronously) invoking the affected EventListeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent()."><code>EventTarget.dispatchEvent()</code></a>.</p>
<p>There are many types of events, some of which use other interfaces based on the main <code>Event</code> interface. <code>Event</code> itself contains the properties and methods which are common to all events.</p>
<p>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 <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element">HTML elements</a> (such as <code><button></code>, <code><div></code>, <code><span></code>, etc.) using <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener" title="The EventTarget method addEventListener() sets up a function that will be called whenever the specified event is delivered to the target."><code>EventTarget.addEventListener()</code></a>, and this generally replaces using the old HTML <a href="https://developer.mozilla.org/en-US/docs/HTML/Global_attributes">event handler attributes</a>. Further, when properly added, such handlers can also be disconnected if needed using <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener" title="The EventTarget.removeEventListener() method removes from the EventTarget an event listener previously registered with EventTarget.addEventListener(). The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal"><code>removeEventListener()</code></a>.</p>
<div class="blockIndicator note">
<p><strong>Note:</strong> 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.)</p>
</div>
<p>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 <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture">Event bubbling and capture</a> settings of each handler triggered.</p>
<h2 id="Introduction" name="Introduction">Interfaces based on Event</h2>
<p>Below is a list of interfaces which are based on the main <code>Event</code> interface, with links to their respective documentation in the MDN API reference.</p>
<p>Note that all event interfaces have names which end in "Event".</p>
<div class="index">
<ul>
<li>{{domxref("AnimationEvent")}}</li>
<li>{{domxref("AudioProcessingEvent")}}</li>
<li>{{domxref("BeforeInputEvent")}}</li>
<li>{{domxref("BeforeUnloadEvent")}}</li>
<li>{{domxref("BlobEvent")}}</li>
<li>{{domxref("ClipboardEvent")}}</li>
<li>{{domxref("CloseEvent")}}</li>
<li>{{domxref("CompositionEvent")}}</li>
<li>{{domxref("CSSFontFaceLoadEvent")}}</li>
<li>{{domxref("CustomEvent")}}</li>
<li>{{domxref("DeviceLightEvent")}}</li>
<li>{{domxref("DeviceMotionEvent")}}</li>
<li>{{domxref("DeviceOrientationEvent")}}</li>
<li>{{domxref("DeviceProximityEvent")}}</li>
<li>{{domxref("DOMTransactionEvent")}}</li>
<li>{{domxref("DragEvent")}}</li>
<li>{{domxref("EditingBeforeInputEvent")}}</li>
<li>{{domxref("ErrorEvent")}}</li>
<li>{{domxref("FetchEvent")}}</li>
<li>{{domxref("FocusEvent")}}</li>
<li>{{domxref("GamepadEvent")}}</li>
<li>{{domxref("HashChangeEvent")}}</li>
<li>{{domxref("IDBVersionChangeEvent")}}</li>
<li>{{domxref("InputEvent")}}</li>
<li>{{domxref("KeyboardEvent")}}</li>
<li>{{domxref("MediaStreamEvent")}}</li>
<li>{{domxref("MessageEvent")}}</li>
<li>{{domxref("MouseEvent")}}</li>
<li>{{domxref("MutationEvent")}}</li>
<li>{{domxref("OfflineAudioCompletionEvent")}}</li>
<li>{{domxref("OverconstrainedError")}}</li>
<li>{{domxref("PageTransitionEvent")}}</li>
<li>{{domxref("PaymentRequestUpdateEvent")}}</li>
<li>{{domxref("PointerEvent")}}</li>
<li>{{domxref("PopStateEvent")}}</li>
<li>{{domxref("ProgressEvent")}}</li>
<li>{{domxref("RelatedEvent")}}</li>
<li>{{domxref("RTCDataChannelEvent")}}</li>
<li>{{domxref("RTCIdentityErrorEvent")}}</li>
<li>{{domxref("RTCIdentityEvent")}}</li>
<li>{{domxref("RTCPeerConnectionIceEvent")}}</li>
<li>{{domxref("SensorEvent")}}</li>
<li>{{domxref("StorageEvent")}}</li>
<li>{{domxref("SVGEvent")}}</li>
<li>{{domxref("SVGZoomEvent")}}</li>
<li>{{domxref("TimeEvent")}}</li>
<li>{{domxref("TouchEvent")}}</li>
<li>{{domxref("TrackEvent")}}</li>
<li>{{domxref("TransitionEvent")}}</li>
<li>{{domxref("UIEvent")}}</li>
<li>{{domxref("UserProximityEvent")}}</li>
<li>{{domxref("WebGLContextEvent")}}</li>
<li>{{domxref("WheelEvent")}}</li>
</ul>
</div>
<h2 id="Constructor" name="Constructor">Constructor</h2>
<dl>
<dt>{{domxref("Event.Event", "Event()")}}</dt>
<dd>Creates an <code>Event</code> object, returning it to the caller.</dd>
</dl>
<h2 id="Properties" name="Properties">Properties</h2>
<dl>
<dt>{{domxref("Event.bubbles")}} {{readonlyinline}}</dt>
<dd>A boolean indicating whether or not the event bubbles up through the DOM.</dd>
<dt>{{domxref("Event.cancelBubble")}}</dt>
<dd>A historical alias to {{domxref("Event.stopPropagation()")}}. Setting its value to <code>true</code> before returning from an event handler prevents propagation of the event.</dd>
<dt>{{domxref("Event.cancelable")}} {{readonlyinline}}</dt>
<dd>A boolean indicating whether the event is cancelable.</dd>
<dt>{{domxref("Event.composed")}} {{ReadOnlyInline}}</dt>
<dd>A boolean indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM.</dd>
<dt>{{domxref("Event.currentTarget")}} {{readonlyinline}}</dt>
<dd>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 <em>retargeting</em>.</dd>
<dt>{{domxref("Event.deepPath")}} {{non-standard_inline}}</dt>
<dd>An {{jsxref("Array")}} of DOM {{domxref("Node")}}s through which the event has bubbled.</dd>
<dt>{{domxref("Event.defaultPrevented")}} {{readonlyinline}}</dt>
<dd>Indicates whether or not the call to {{domxref("event.preventDefault()")}} canceled the event.</dd>
<dt>{{domxref("Event.eventPhase")}} {{readonlyinline}}</dt>
<dd>Indicates which phase of the event flow is being processed.</dd>
<dt>{{domxref("Event.explicitOriginalTarget")}} {{non-standard_inline}} {{readonlyinline}}</dt>
<dd>The explicit original target of the event (Mozilla-specific.)</dd>
<dt>{{domxref("Event.originalTarget")}} {{non-standard_inline}} {{readonlyinline}}</dt>
<dd>The original target of the event, before any retargetings. (Mozilla-specific.)</dd>
<dt>{{domxref("Event.returnValue")}}</dt>
<dd>A historical property introduced by Internet Explorer and eventually adopted into the DOM specification in order to ensure existing sites continue to work. Ideally, you should try to use {{domxref("Event.preventDefault()")}} and {{domxref("Event.defaultPrevented")}} instead, but you can use <code>returnValue</code> if you choose to do so.</dd>
<dt>{{domxref("Event.srcElement")}} {{non-standard_inline}}</dt>
<dd>A non-standard alias (from old versions of Microsoft Internet Explorer) for {{domxref("Event.target")}}. Some other browsers are starting to support it for web compatibility purposes.</dd>
<dt>{{domxref("Event.target")}} {{readonlyinline}}</dt>
<dd>A reference to the target to which the event was originally dispatched.</dd>
<dt>{{domxref("Event.timeStamp")}} {{readonlyinline}}</dt>
<dd>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 {{domxref("DOMHighResTimeStamp")}} instead.</dd>
<dt>{{domxref("Event.type")}} {{readonlyinline}}</dt>
<dd>The name of the event. Case-insensitive.</dd>
<dt>{{domxref("Event.isTrusted")}} {{readonlyinline}}</dt>
<dd>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, like {{domxref("Event.initEvent")}}).</dd>
</dl>
<h3 id="Obsolete_properties">Obsolete properties</h3>
<dl>
<dt>{{domxref("Event.scoped")}} {{readonlyinline}} {{obsolete_inline}}</dt>
<dd>Obsolete; use {{domxref("Event.composed", "composed")}} instead. (A {{jsxref("Boolean")}} indicating whether the given event will bubble across through the shadow root into the standard DOM.)</dd>
</dl>
<h2 id="Methods" name="Methods">Methods</h2>
<dl>
<dt>{{domxref("Event.createEvent()")}} {{deprecated_inline}}</dt>
<dd>
<p>Creates a new event, which must then be initialized by calling its <code>initEvent()</code> method.</p>
</dd>
<dt>{{domxref("Event.composedPath()")}}</dt>
<dd>Returns the event’s path (objects on which listeners will be invoked). This does not include nodes in shadow trees if the shadow root was created with its {{domxref("ShadowRoot.mode")}} closed.</dd>
</dl>
<dl>
<dt>{{domxref("Event.initEvent()")}} {{deprecated_inline}}</dt>
<dd>Initializes the value of an Event created. If the event has already been dispatched, this method does nothing.</dd>
<dt>{{domxref("Event.preventDefault()")}}</dt>
<dd>Cancels the event (if it is cancelable).</dd>
<dt>{{domxref("Event.stopImmediatePropagation()")}}</dt>
<dd>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).</dd>
<dt>{{domxref("Event.stopPropagation()")}}</dt>
<dd>Stops the propagation of events further along in the DOM.</dd>
</dl>
<h3 id="Obsolete_methods">Obsolete methods</h3>
<dl>
<dt>{{domxref("Event.getPreventDefault()")}} {{non-standard_inline}}</dt>
<dd>Non-standard; use {{domxref("Event.defaultPrevented")}} instead. (Returns the value of {{domxref("Event.defaultPrevented")}}.)</dd>
<dt>{{domxref("Event.preventBubble()")}} {{non-standard_inline}} {{Obsolete_inline(24)}}</dt>
<dd>Obsolete; use {{domxref("event.stopPropagation")}} instead. (Prevents the event from bubbling.)</dd>
<dt>{{domxref("Event.preventCapture()")}} {{non-standard_inline}} {{Obsolete_inline(24)}}</dt>
<dd>Obsolete; use {{domxref("event.stopPropagation")}} instead.</dd>
</dl>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG', '#interface-event', 'Event')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Event")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>Types of events available: <a href="/en-US/docs/Web/Reference/Events">Event reference</a></li>
<li><a href="/en-US/docs/Web/API/Event/Comparison_of_Event_Targets">Comparison of Event Targets</a> (<code>target</code> vs <code>currentTarget</code> vs <code>relatedTarget</code> vs <code>originalTarget</code>)</li>
<li><a href="/en-US/docs/Web/Guide/DOM/Events/Creating_and_triggering_events">Creating and triggering custom events</a></li>
<li>For Firefox add-on developers:
<ul>
<li><a href="/en-US/docs/Listening_to_events_in_Firefox_extensions">Listening to events in Firefox extensions</a></li>
<li><a href="/en-US/docs/Listening_to_events_on_all_tabs">Listening to events on all tabs</a></li>
</ul>
</li>
</ul>
|