Custom Events & EventTarget API
Also Known As
CustomEvent
EventTarget
custom DOM events
event emitter
TL;DR
Creating and dispatching custom DOM events for decoupled component communication — alternatives to direct function calls and third-party event emitters.
Explanation
CustomEvent allows any DOM element to dispatch events with arbitrary data in the detail property. EventTarget can be subclassed to create a non-DOM event emitter. Patterns: component A dispatches 'cart:updated' on document, component B listens without knowing about A. This decouples components without shared state or direct references. The EventTarget mixin also enables event-driven communication in Web Workers and custom objects. Bubbling and capture phases allow event delegation.
Common Misconception
✗ Custom events are only useful in large frameworks — they are valuable in any multi-component page for decoupling components that need to communicate without direct references.
Why It Matters
Custom events enable decoupled communication between independent page components — a checkout widget can react to cart updates without importing or referencing the cart module.
Common Mistakes
- Not setting bubbles: true when cross-component communication is needed — non-bubbling events don't propagate.
- Not removing event listeners — addEventListener without removeEventListener causes memory leaks.
- Dispatching events before the target element exists in the DOM.
- Using string event names without a naming convention — namespace with colon: 'cart:updated', 'user:login'.
Code Examples
✗ Vulnerable
// Tight coupling — direct function calls:
class Cart {
update(items) {
this.items = items;
checkout.refresh(); // Direct reference — Cart knows about Checkout
analytics.track(); // Cart knows about Analytics
header.updateBadge(); // Cart knows about Header
}
}
✓ Fixed
// Decoupled via custom events:
class Cart {
update(items) {
this.items = items;
document.dispatchEvent(new CustomEvent('cart:updated', {
bubbles: true,
detail: { items, count: items.length }
}));
// Cart knows nothing about who listens
}
}
// Independent listeners:
document.addEventListener('cart:updated', e => checkout.refresh(e.detail));
document.addEventListener('cart:updated', e => analytics.track(e.detail));
document.addEventListener('cart:updated', e => header.updateBadge(e.detail.count));
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
62
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 16
Amazonbot 16
Google 10
ChatGPT 3
Unknown AI 3
Ahrefs 2
Majestic 2
DuckDuckGo 2
SEMrush 2
Also referenced
How they use it
crawler 52
crawler_json 3
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Use CustomEvent to communicate between PHP-rendered components without coupling them — one component dispatches, another listens, and neither knows about the other
📦 Applies To
javascript ES2015
web
🔗 Prerequisites
🔍 Detection Hints
Direct function calls between unrelated components; global variables used as communication channel; tightly coupled vanilla JS components
Auto-detectable:
✗ No
eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Low
Context: Function