CustomEvent()

The CustomEvent() constructor creates a new CustomEvent.

Syntax

CustomEvent(typeArg);
CustomEvent(typeArg, options);

Parameters

typeArg

A string representing the name of the event.

options Optional

An object, with the following fields:

  • "detail", optional and defaulting to null, of any type, containing an event-dependent value associated with the event. This is available to the handler using the CustomEvent.detail property.
  • Any field that can be used in the init object of the Event() constructor.

Return value

A new CustomEvent object of the specified type, with any other properties configured according to the dictionary if one was provided.

Example

// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) });

// create and dispatch the event
var event = new CustomEvent("cat", {
  detail: {
    hazcheeseburger: true
  }
});
obj.dispatchEvent(event);

Additional examples can be found at Creating and triggering events.

Specifications

Specification
DOM Standard (DOM)
# ref-for-dom-customevent-customevent

Browser compatibility

BCD tables only load in the browser

See also