1. What are events?
An event is something that happens in the browser โ a user clicks a button, types in an input field, scrolls the page, or submits a form. The browser detects these interactions and fires events that your JavaScript code can listen for and respond to.
Think about shopping on UrbanCart: you click "Add to Cart," type a search query, change a quantity dropdown, submit the checkout form, and scroll through product listings. Each of these user actions fires a browser event that JavaScript can intercept and handle.
2. addEventListener
addEventListener is the modern, recommended way to attach event handlers to DOM elements. It takes the event type as a string and a callback function that runs when the event fires.
btn.onclick = function() {...} only allows one handler per event. If you assign a second onclick, it overwrites the first. addEventListener lets you attach multiple handlers to the same event on the same element.
removeEventListener with the same function reference to detach a handler. Anonymous/arrow functions can't be removed because you have no reference to pass back.
3. Common events
JavaScript has dozens of event types. Here are the ones you'll use most often when building UrbanCart features:
| Event | Fires when... | UrbanCart example |
|---|---|---|
click |
User clicks an element | "Add to Cart" button |
submit |
Form is submitted | Checkout form, login form |
input |
Value changes (every keystroke) | Live search bar filtering |
change |
Value changes and element loses focus | Quantity dropdown, sort selector |
keydown |
A key is pressed down | Keyboard shortcuts (Esc to close modal) |
mouseover |
Mouse enters an element | Product card hover preview |
focus / blur |
Element gains or loses focus | Highlight search input, validate on blur |
4. The event object
Every time an event fires, the browser creates an event object and passes it to your handler function as the first argument. This object contains useful information about what happened, where it happened, and what element triggered it.
event.target
The actual element that triggered the event. If you click a <span> inside a <button>, target is the span.
event.currentTarget
The element the listener is attached to. Always the element you called addEventListener on.
5. event.preventDefault()
Some events have default browser behaviors: form submissions reload the page, links navigate away, right-clicks open a context menu. event.preventDefault() stops these defaults so you can handle everything in JavaScript instead.
preventDefault() to handle submission in JavaScript instead โ validate, send via fetch, and show a success message without leaving the page.
6. Event delegation
Event delegation is a pattern where you attach a single event listener to a parent element instead of adding separate listeners to each child. When a child is clicked, the event bubbles up to the parent, where your single listener catches it.
7. Bubbling vs capturing
When an event fires on an element, it doesn't just stay there. It propagates through the DOM in two phases: capturing (top-down) and bubbling (bottom-up). Understanding this is what makes event delegation work.
document โ parent โ target
Event fires here
target โ parent โ document
Try It Yourself #1
Build an "Add to Cart" interaction: click buttons to add products to the cart, and watch the cart counter update. Change the quantity input to see the change event in action.
Try It Yourself #2
Build a live product search: as the user types, the input event filters the product list in real time. Products that don't match the query are hidden instantly.
Test your knowledge
addEventListener do that onclick cannot?e.preventDefault() on a form's submit event?๐ Challenge: UrbanCart mini checkout
Combine everything into a mini checkout:
- Use event delegation on a product list โ one listener handles all "Add to Cart" buttons
- Add a checkout
<form>with name and email. UsepreventDefault()to handle submission in JS - Validate fields are not empty. Use
inputevent to clear errors as the user types
Bonus: Add a keydown listener for "Enter" to jump to checkout.
You just mastered JavaScript events.
Now build a full-stack career.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ