Events
Events are how the browser tells your JavaScript that something happened — a click, a keypress, a form submission, or a network response. Mastering the event model is essential for building interactive UIs.
How do you handle events in JavaScript?
Attach a function with element.addEventListener("click", handler). The handler receives an event object with details such as event.target (what was clicked) and event.key (which key was pressed). Call event.preventDefault() to stop default behaviour like a form submitting or a link navigating.
const button = document.querySelector("#save");
button.addEventListener("click", (event) => {
console.log("clicked", event.target);
});
form.addEventListener("submit", (e) => {
e.preventDefault();
save(new FormData(form));
});
input.addEventListener("input", (e) => console.log(e.target.value));Event Propagation Phases
Event travels DOWN from window → document → html → ... → target. Listeners with { capture: true }.
Event is at the target element. Both capture and bubble listeners fire here.
Event travels UP from target → ... → html → document → window. Default behavior.
Key Event Methods
e.preventDefault()Prevent default browser action (link navigation, form submit…)e.stopPropagation()Stop event from bubbling/capturing furthere.stopImmediatePropagation()Also stop other listeners on the same elemente.targetThe original element that triggered the evente.currentTargetThe element the listener is attached toEssential Event Types
Mouse
clickdblclickmousedown/upmousemovemouseenter/leavecontextmenu
Keyboard
keydownkeyupkey property (e.g. Enter)code property (e.g. KeyA)Modifier keys: ctrlKey, shiftKeye.preventDefault() for shortcuts
Form & Focus
input (every keystroke)change (on commit/blur)submit (form)focus / blurfocusin/out (bubbling)reset
Document
DOMContentLoadedload (full page)beforeunload / unloadvisibilitychangeresizescroll
Pointer
pointerdown/up/movepointerenter/leavepointercancelgotpointercaptureUnifies mouse + touch + stylusCheck e.pointerType
Custom
new CustomEvent('name', {detail})el.dispatchEvent(event)bubbles: true/falsecancelable: true/falsee.detail for payloadEvent bus pattern
Common Pitfalls
Interactive Events Demo
Code Editor
<button id="btn">Click</button>
<input id="field" placeholder="Type, then press Enter">
<a id="link" href="https://example.com">Link (navigation prevented)</a>
<pre id="out"></pre>Preview
Browser events — step by step
Event propagation — capture, target, and bubble
When you click a nested element, the event travels in three phases. Capture (top-down): from window down to the target. Target: the element itself receives the event. Bubble (bottom-up): event propagates back up to window. By default, addEventListener listens in the bubble phase. Pass true as the third argument to listen in the capture phase.
🎯 Quick Check
What does event.stopPropagation() do?
JavaScript Events: summary
Key points
- Common events:
click,input,change,submit,keydown,focus,blur,scroll,DOMContentLoaded. - Events bubble up from the target to its ancestors;
stopPropagation()stops that. - Remove listeners with
removeEventListener(same function reference) or anAbortControllersignal. - Use
{ passive: true }for scroll and touch listeners so they don't delay scrolling.
Frequently asked questions
›What is the difference between the input and change events?
input fires on every keystroke or value change. change fires when the user commits the change, such as on blur for text fields or on selection for checkboxes and selects.
›Should I use onclick or addEventListener?
addEventListener. It allows several handlers, options like once and passive, and keeps JavaScript out of your HTML.