Introduction: Why Static Websites Feel Dead
Imagine you visit a website, but no matter what you do, clicking buttons, hovering over images, or pressing keys, nothing happens.
Wouldn’t that feel like interacting with a piece of paper rather than a digital experience?
This is why JavaScript events exist! They allow websites to respond to user actions, making them feel alive and engaging. Whether it’s clicking a button, submitting a form, or scrolling down a page, events are what make websites interactive.
In this guide, I’ll take you step by step through JavaScript events—from the basics to advanced concepts like event bubbling and delegation. By the end, you’ll have the confidence to add interactivity to any website.
Let’s start with the most important question:
What Are JavaScript Events?
In simple terms, a JavaScript event is any action that happens on a webpage that JavaScript can respond to.
Think of events as triggers—whenever something happens, JavaScript can take action.
Some events are caused by users, like:
✔ click – Clicking a button or link
✔ mouseover – Hovering over an element
✔ keydown – Pressing a key on the keyboard
✔ submit – Submitting a form
✔ scroll – Scrolling down a page
Others happen automatically, like:
✔ load – When a webpage finishes loading
✔ error – When an error occurs
Real-Life Example: The Light Switch Analogy
Imagine a light switch in a room:
- Flipping the switch (event) → Turns the light on (response)
- Flipping it again (event) → Turns the light off (response)
Similarly, JavaScript listens for events and executes code when they occur.
Alright, now that we know what events are, let’s see how we can handle them in JavaScript.
How to Handle Events in JavaScript
There are multiple ways to add event listeners to elements. Let’s go through them in order of worst to best practices.
1. Inline Event Handlers (Not Recommended)
This method places JavaScript directly inside HTML, which is messy and hard to maintain.

Why avoid this?
- It mixes JavaScript with HTML, making code harder to read.
- If you need to update the function, you have to modify the HTML too.
Since this method is bad practice, let’s see a better approach.
2. Using Event Properties (Better, But Still Limited)
A cleaner way is to use JavaScript to assign events instead of writing them inside HTML.

Downside:
- You can only assign one function per event. If you assign another function, it overwrites the previous one.
So what if we want multiple functions on the same event? This brings us to the best method.
3. Using addEventListener (Best Practice ✅)
The best way to handle events is using addEventListener()
, which allows multiple event handlers without overwriting previous ones.

With addEventListener()
, you can attach multiple event handlers to the same element without overwriting previous ones.
Common JavaScript Events and How to Use Them
1. Click Event
This is one of the most frequently used events. It’s triggered when a user clicks an element.

2. Mouseover Event
Triggers when the user hovers over an element.

3. Keydown Event (Typing Detection)
Useful for keyboard shortcuts or form interactions.

Great! Now let’s level up and talk about something you’ll definitely face in real-world projects:
Event Bubbling and Event Delegation
Why Do We Need This?
Sometimes, events don’t behave as expected. If you have multiple elements inside each other, an event might trigger for both the child and the parent—causing unexpected results.
What is Event Bubbling?
Let’s say you click a button inside a <div>
. Normally, the event bubbles up from the button to the div.

How to Stop Bubbling?
Use event.stopPropagation()
to prevent the event from reaching parent elements.
What is Event Delegation? (Very Useful in Large Apps)
Instead of adding event listeners to every child, you can add one listener to the parent and detect which child was clicked.

Why use this?
- Improves performance (especially in large lists).
- Works even if elements are dynamically added later.
Conclusion: Bring Your Website to Life!
JavaScript events turn static pages into interactive experiences. Whether handling clicks, detecting key presses, or managing form submissions, events are essential for modern web development.
Now it’s your turn! Try adding some events to your next project and see how they make your website feel alive.
Exciting news! Odurinde.com is launching soon, and we’re also gearing up for a FREE 2-day web development training!
Stay tuned, we’ll be making an official announcement soon with details on how to register and everything you need to know. Don’t miss out!