2.6 Event Handling – Making Web Pages Interactive (So Simple!)

Table of Contents

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 TypeWhat HappensExample
clickUser clicks an elementClick a button
mouseoverMouse hovers over an elementHover over a menu
keydownA key is pressed on the keyboardPress Enter in a form
submitA form is submittedSend login form
scrollUser scrolls the pageScrolling Instagram feed
changeAn input field value changesSelect a different dropdown option

In short: events happen all the time! 🌟


Word Image 28
2.6 Event Handling – Making Web Pages Interactive (So Simple!) 5


πŸ‘‚ 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? βœ…


Visual: User Clicking A Button β†’ Alert Box Pops Up
2.6 Event Handling – Making Web Pages Interactive (So Simple!) 6


🚦 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!

Arrow Diagram Showing Bubbling: Button β†’ Div β†’ Document&Quot;]
2.6 Event Handling – Making Web Pages Interactive (So Simple!) 7


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.


Infographic: Preventdefault Vs Stoppropagation With Code + Diagram
2.6 Event Handling – Making Web Pages Interactive (So Simple!) 8

🧠 Key Concepts Recap

ConceptWhat It DoesExample
EventAn action that happens on the pageClick, hover, type
Event ListenerCode that waits for an eventaddEventListener("click", ...)
BubblingEvent moves up from inner to outerButton click bubbles to div
CapturingEvent moves down from outer to innerDiv captures click before button
preventDefault()Stops default action (like form reload)Handle form in JavaScript only
stopPropagation()Stops the event from bubbling furtherStop 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! πŸ’ͺ🌐

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts