Dispatches an Event at the specified EventTarget, 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().
Syntax
cancelled = !target.dispatchEvent(event)
Parameter
eventis theEventobject to be dispatched.targetis used to initialize theEvent.targetand determine which event listeners to invoke.
Return Value
- The return value is
falseif event is cancelable and at least one of the event handlers which handled this event calledEvent.preventDefault(). Otherwise it returnstrue.
The dispatchEvent method throws UNSPECIFIED_EVENT_TYPE_ERR if the event's type was not specified by initializing the event before the method was called, or if the event's type is null or an empty string. Exceptions thrown by event handlers are reported as uncaught exceptions; the event handlers run on a nested callstack: they block the caller until they complete, but exceptions do not propagate to the caller.
Notes
dispatchEvent is the last step of the create-init-dispatch process, which is used for dispatching events into the implementation's event model. The event can be created using Event constructor
See also the Event object reference.
Example
See Creating and triggering events.
Specifications
| Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'EventTarget.dispatchEvent()' in that specification. |
Living Standard | Initial definition in the DOM 2 Events specification. |
Browser compatibility
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | 4 | (Yes) | 2 | 9 [1] | 9.64 (probably earlier) | 3.2 (probably earlier) |
| Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | ? | (Yes) | ? | ? | ? | ? |
[1]: Earlier versions of IE instead only support the proprietary EventTarget.fireEvent() method.