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.

๐Ÿ›’
UrbanCart Example
Events power every interaction

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.

events-intro.js The simplest event
Event-driven programming: JavaScript sets up listeners that wait for events. When an event fires, the browser calls your handler function. This is the foundation of all interactive web apps.

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.

listeners.js addEventListener syntax
Why not onclick? Using 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.
Removing listeners: Use 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
common-events.js Practical examples

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-object.js Key properties
๐ŸŽฏ

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.

UrbanCart checkout: When a customer submits the checkout form, the browser reloads the page by default. We call preventDefault() to handle submission in JavaScript instead โ€” validate, send via fetch, and show a success message without leaving the page.
prevent-default.js Stop form reload

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.

delegation.js One listener, many buttons
Why delegation wins: (1) Better performance โ€” 1 listener vs hundreds. (2) Works with dynamic content โ€” new elements added via JS automatically inherit the behavior. (3) Less code to manage. This is the pattern React and other frameworks use internally.

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.

Event Propagation Phases
1. Capturing
document โ†’ parent โ†’ target
โ†’
2. Target
Event fires here
โ†’
3. Bubbling
target โ†’ parent โ†’ document
bubbling.js Bubbling (default) vs Capturing

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 โ€” Add to Cart Events
Output

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.

๐Ÿ› ๏ธ Try It Yourself โ€” Live Product Search
Output

Test your knowledge

Question 1 of 5
What does addEventListener do that onclick cannot?
Question 2 of 5
Which property of the event object gives you the actual element that was clicked?
Question 3 of 5
Why do you call e.preventDefault() on a form's submit event?
Question 4 of 5
What is event delegation?
Question 5 of 5
In which direction does an event travel during the bubbling phase?
0
/5
Great job!

๐Ÿ† Challenge: UrbanCart mini checkout

Combine everything into a mini checkout:

  1. Use event delegation on a product list โ€” one listener handles all "Add to Cart" buttons
  2. Add a checkout <form> with name and email. Use preventDefault() to handle submission in JS
  3. Validate fields are not empty. Use input event to clear errors as the user types

Bonus: Add a keydown listener for "Enter" to jump to checkout.

Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’