Hey friends! π Iβm Vikas Sankhla, your tech buddy from the Web Codder YouTube channel. Today weβre learning how to make web pages ALIVE with event handling.
Think about this:
- Clicking a button π±οΈ
- Typing in a form β¨οΈ
- Hovering your mouse over a menu π―
These are all events β little things that happen on your page. And guess what? JavaScript can listen and react to them! Letβs learn how. π
π¬ What Are Events in JavaScript?
An event is any action the user does on the page.
Here are some common events:
Event Type | What Happens | Example |
---|---|---|
click | User clicks an element | Click a button |
mouseover | Mouse hovers over an element | Hover over a menu |
keydown | A key is pressed on the keyboard | Press Enter in a form |
submit | A form is submitted | Send login form |
scroll | User scrolls the page | Scrolling Instagram feed |
change | An input field value changes | Select a different dropdown option |
In short: events happen all the time! π

π Event Listeners β How JavaScript Listens π
We use event listeners to tell JavaScript:
βHey, when THIS happens, DO THIS!β
The best way? Using addEventListener()
.
Basic Example:
htmlCopyEdit<button id="myButton">Click Me!</button>
JavaScript:
jsCopyEditlet btn = document.getElementById("myButton");
btn.addEventListener("click", function() {
alert("You clicked the button! π");
});
π What happens:
- You click the button β‘οΈ you get an alert popup.
Easy, right? β

π¦ Different Types of Events
Letβs quickly look at different events and how to handle them.
1οΈβ£ Click Event
jsCopyEditdocument.getElementById("myButton").addEventListener("click", function() {
console.log("Button was clicked!");
});
2οΈβ£ Mouseover (Hover)
jsCopyEditdocument.getElementById("myDiv").addEventListener("mouseover", function() {
console.log("Mouse is over the div!");
});
3οΈβ£ Keydown
jsCopyEditdocument.addEventListener("keydown", function(event) {
console.log("You pressed: " + event.key);
});
4οΈβ£ Submit
jsCopyEditdocument.getElementById("myForm").addEventListener("submit", function(e) {
e.preventDefault(); // Stops the form from reloading the page
console.log("Form submitted!");
});
π Event Bubbling and Capturing
Okay, hereβs where it gets a bit tricky (but donβt worry, Iβll keep it simple π).
Imagine you have this:
htmlCopyEdit<div id="outer">
<button id="inner">Click Me</button>
</div>
When you click the button:
1οΈβ£ The buttonβs click event fires first
2οΈβ£ Then the divβs click event fires
This is called bubbling β the event bubbles UP from the inside to the outside.
Example:
jsCopyEditdocument.getElementById("inner").addEventListener("click", function() {
console.log("Button clicked!");
});
document.getElementById("outer").addEventListener("click", function() {
console.log("DIV clicked!");
});
Click the button β‘οΈ Output:
cssCopyEditButton clicked!
DIV clicked!
![2.6 Event Handling β Making Web Pages Interactive (So Simple!) 3 Arrow Diagram Showing Bubbling: Button β Div β Document&Quot;]](https://webcodder.dev/wp-content/uploads/2025/05/1_ePPmZzDbI9fJSHSmDKODqg-950x1024.jpg)
Capturing (Less Common)
You can also capture events on the way down (from outer to inner).
We donβt use it often, but hereβs how:
jsCopyEditdocument.getElementById("outer").addEventListener("click", function() {
console.log("DIV clicked (capturing)!");
}, true);
The true
means: capture mode.
π Preventing Default and Stopping Propagation
1οΈβ£ event.preventDefault()
Some elements have default behavior.
Example: Submitting a form reloads the page.
We can stop that like this:
jsCopyEditdocument.getElementById("myForm").addEventListener("submit", function(e) {
e.preventDefault(); // Stop reload
console.log("Handled without reload!");
});
2οΈβ£ event.stopPropagation()
Sometimes you donβt want the event to bubble up.
Example:
jsCopyEditdocument.getElementById("inner").addEventListener("click", function(e) {
console.log("Only the button!");
e.stopPropagation(); // Stops bubbling
});
Now, clicking the button only logs “Only the button!”, and the outer DIV doesnβt react.

π§ Key Concepts Recap
Concept | What It Does | Example |
---|---|---|
Event | An action that happens on the page | Click, hover, type |
Event Listener | Code that waits for an event | addEventListener("click", ...) |
Bubbling | Event moves up from inner to outer | Button click bubbles to div |
Capturing | Event moves down from outer to inner | Div captures click before button |
preventDefault() | Stops default action (like form reload) | Handle form in JavaScript only |
stopPropagation() | Stops the event from bubbling further | Stop div from reacting to button click |
π― Conclusion
Wow! Now you know how to make your web pages interactive and smart. π You learned:
- What events are
- How to add event listeners
- How bubbling & capturing work
- How to control event behavior
π Keep experimenting with different events β itβs the best way to learn. π οΈ
For more epic tutorials:
π₯ Subscribe on YouTube: https://www.youtube.com/@web_codder/?sub_confirmation=1
πΈ Follow on Instagram: https://www.instagram.com/web_codder_official/
π¬ Join our WhatsApp group: https://webcodder.dev/whatsapp
Letβs keep making the web AWESOME together! πͺπ