- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Custom Events
JavaScript Events
JavaScript Custom Events
A custom event lets one part of your code announce something to another.
You are not limited to the events the browser provides.
CustomEvent creates your own, with any data attached.
Example
Example
javascript
document.body.innerHTML = '<div id="cart"></div>';
const cart = document.getElementById("cart");
cart.addEventListener("itemAdded", function (event) {
console.log("added " + event.detail.name);
});
cart.dispatchEvent(new CustomEvent("itemAdded", {
detail: { name: "Book" }
}));