EventTarget

EventTarget.prototype.dispatchEvent

Dispatches an event to this EventTarget, invoking the affected event listeners in the appropriate order

Syntax

JavaScript
target.dispatchEvent(event)

Parameters

ParameterTypeDescription
eventEventThe Event object to dispatch

Return Value

false if the event is cancelable and at least one handler called preventDefault(), otherwise true

Examples

Basic Usage
const el = document.querySelector('.btn')!
const event = new Event('click', { bubbles: true })
el.dispatchEvent(event)
Practical Example
const custom = new CustomEvent('user:login', {
  detail: { username: 'alice' },
  bubbles: true
})
document.dispatchEvent(custom)
Advanced Usage
function emit(el: HTMLElement, name: string, data?: unknown) {
  const event = new CustomEvent(name, {
    detail: data,
    bubbles: true,
    cancelable: true
  })
  return el.dispatchEvent(event)
}

Understanding EventTarget.prototype.dispatchEvent

The EventTarget.prototype.dispatchEvent method in JavaScript dispatches an event to this EventTarget, invoking the affected event listeners in the appropriate order. It belongs to the EventTarget object and is one of the most widely used methods for working with eventtarget values in modern JavaScript and TypeScript applications.

The method signature is target.dispatchEvent(event). It accepts 1 parameter: event. When called, it returns false if the event is cancelable and at least one handler called preventdefault(), otherwise true. Understanding when and how to use dispatchEvent() helps you write more expressive, readable code.

Common use cases for EventTarget.prototype.dispatchEvent include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like event-addeventlistener, event-customevent, event-preventdefault, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for EventTarget.prototype.dispatchEvent is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More EventTarget Methods

Other methods in the EventTarget object

Related Tools

More EventTarget Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.