1. What Are Events?
An Event is a signal fired by the browser whenever something happens on the page. Events allow your JavaScript code to react to user interactions like button clicks, typing in forms, moving the mouse, pressing keys, or page lifecycle changes like window resizing.
2. How Events Work in the Browser
1. Trigger
User clicks, types, or scrolls on the page.
2. Dispatch & Bubbling
Browser creates Event Object and propagates through DOM tree.
3. Callback Handler
Your registered JavaScript function executes and updates the UI.
3. Event Listeners: addEventListener()
// Modern standard syntax
btn.addEventListener("click", (event) => {
console.log("Button clicked!");
});
4. Common Mouse Events
| Event | When It Triggers | Example Use Case |
|---|---|---|
click | Primary mouse click / tap | Opening modals, submitting buttons |
dblclick | Rapid double click | Inline text editing, zoom toggles |
mouseover / mouseout | Cursor enters or exits element bounds | Hover tooltips, menu previews |
mousemove | Cursor moves inside element | Custom cursor effects, canvas drawing |
5. Keyboard Events: keydown & keyup
console.log("Key Pressed:", event.key); // e.g. "Escape", "Enter"
if (event.key === "Escape") {
closeModal();
}
});
6. Form & Focus Events
input Event
Fires synchronously on every character typed or deleted. Ideal for live search filters.
submit Event
Fires on form submission. Always pair with event.preventDefault() to prevent page reloads.
7. The Event Object: target & preventDefault
event.preventDefault(); // Prevents full-page browser refresh
console.log("Event Type:", event.type); // "submit"
console.log("Event Target:", event.target); // The <form> element
});
8. Event Bubbling: Flowing Up the Tree
When you click a button inside a container, the event fires on the button first, then bubbles upwards through its parent container, through <body>, to document, and finally to window. You can halt this propagation using event.stopPropagation().
9. Event Capturing Phase
container.addEventListener("click", handleCapture, { capture: true });
10. Event Delegation: 1 Listener to Rule Them All
taskList.addEventListener("click", (event) => {
if (event.target.classList.contains("delete-btn")) {
event.target.closest("li").remove();
}
});
11. Removing Event Listeners with Named Functions
// Add listener with named reference
window.addEventListener("keydown", handleKey);
// Clean up / remove listener
window.removeEventListener("keydown", handleKey);
12. Building Interactive UI With Events
textarea.addEventListener("input", (e) => {
const count = e.target.value.length;
counterDisplay.textContent = `${count} / 200 characters`;
});
13. Common Event Handling Mistakes
- Accidentally invoking the function immediately: Writing
btn.addEventListener('click', handleClick())executes it on page load! PasshandleClickwithout parentheses. - Forgetting preventDefault() on forms: Causes unexpected browser reloads.
- Trying to remove anonymous listeners:
removeEventListener('click', () => {})never works because anonymous function instances create unique memory references.
14. Events Clean Code Best Practices
- Always use Event Delegation for dynamic item lists and tables.
- Clean up event listeners when components unmount or elements are destroyed to prevent memory leaks.
- Use
{ passive: true }for scroll and touch listeners to maximize frame rate performance.