Web APIs

CustomEvent()

Try the free JavaScript compiler

Constructs a named event with optional detail and inherited EventInit settings.

Formal Syntax & Structure

javascript syntaxjavascript
new CustomEvent(type, options?)

Parameters & Attributes

NameTypeDescription
typestringEvent type.
optionsobjectOptional detail and event settings.

Return Value

CustomEvent

New event object.

Working Example

Example Snippetjavascript
const p=document.createElement("div"),b=document.createElement("button");p.append(b);p.addEventListener("stock-change",e=>console.log(e.detail.qty));b.dispatchEvent(new CustomEvent("stock-change",{detail:{qty:4},bubbles:true}));
JavaScript Reference