JavaScript events
An event is a signal from the browser that something has happened: the user clicked, typed, scrolled or resized the window, a form was submitted, an image finished loading, or a network request completed. JavaScript lets you listen for these signals and respond. Understanding which events exist and how they travel through the page helps you choose the right one and attach your listener in the right place.
Common event families
| Category | Examples | Fired when |
|---|---|---|
| Mouse and pointer | click, dblclick, pointerdown, mouseover | A user clicks, taps or moves the pointer |
| Keyboard | keydown, keyup | A key is pressed or released |
| Form | input, change, submit, focus, blur | Fields change, gain or lose focus, or a form is sent |
| Document and window | DOMContentLoaded, load, resize, scroll | The page loads or the viewport changes |
| Clipboard and drag | copy, paste, dragstart, drop | Content is copied, pasted or dragged |
Pointer events such as pointerdown cover mouse, touch and pen with one set of listeners, which makes them a good choice for interfaces that must work on phones.
How an event travels: capture and bubble
When you click a button inside a list inside a section, the click does not belong only to the button. The event first travels down from the window to the button (the capture phase), reaches the button (the target phase), and then travels back up through every ancestor (the bubble phase). Listeners run during the bubble phase by default, so a click listener on the section also fires when the button is clicked.
Calling event.stopPropagation() stops the event from moving further. Use it sparingly, because other code, such as a dropdown that closes when you click outside it, may rely on the event reaching the document.
Event delegation
Bubbling makes a powerful pattern possible: instead of adding a listener to every item, add one to the parent and check where the event came from. This also works for items added later.
const todoList = document.querySelector('#todos');
todoList.addEventListener('click', event => {
const removeBtn = event.target.closest('button.remove');
if (!removeBtn || !todoList.contains(removeBtn)) return;
removeBtn.closest('li').remove();
});
document.addEventListener('keydown', event => {
if (event.key === 'Escape') {
document.querySelector('.modal')?.classList.remove('open');
}
});
event.target may be an icon or span inside the button, so closest() walks up to find the button itself.
Events that do not bubble, and other surprises
Not every event bubbles. focus and blur stay on their element; use focusin and focusout if you want to delegate them. mouseenter and mouseleave also do not bubble, unlike mouseover and mouseout.
Events such as scroll, resize, input and pointermove can fire dozens of times per second. Heavy work inside them makes a page feel sluggish. Keep those handlers light, or limit how often they run with a debounce or throttle function.
Also note that keypress is deprecated. Use keydown and read event.key.
Main ideas
- Pick the event that matches the user action precisely.
- Events move down, hit the target, then bubble up.
- Delegate to a parent for lists and dynamic content.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.