Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
What is the Event Driven Programming Paradigm ?
Next article icon

What is Event propagation, capturing, bubbling ?

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn about Event Propagation, Capturing, and Bubbling.

Event Propagation determines in which order the elements receive the event. There are two ways to handle this event propagation order of HTML DOM is Event Bubbling and Event Capturing.

For example, suppose there are three components namely component1, component2, component3. Inside these components, we attached the onClickEventListener event handlers. Now when we click on component3 the event handler for all the three components will be executed. Now here the question is in which order the event will execute. Now at this point event bubbling and capturing comes into the picture.

Bubbling:

When an event happens on a component, it first runs the event handler on it, then on its parent component, then all the way up on other ancestors’ components. By default, all event handles through this order from center component event to outermost component event.

Example: In this example, we will create three div components and each component will have an event handler. When we click on component 3, it will give an alert as “component 3 event clicked” then all the way up “component 2 event clicked” and “component 1 event clicked” alerts will display. This is how event bubbling happens from the “bottom to top”.

HTML
<!DOCTYPE html> <html>  <head>     <style>         #div1 {             background-color: lightgreen;             padding: 24px;             border: 1px solid black;         }          #div2 {             background-color: yellow;             padding: 18px;             border: 1px solid black;          }          #div3 {             background-color: orange;             border: 1px solid black;         }     </style> </head>  <body>     <h1 style="color: green">GeeksForGeeks</h1>     <h3>What is Event propagation, capturing, bubbling?</h3>      <div id="div1">         Component 1         <div id="div2">             component 2             <div id="div3">                 component 3             </div>         </div>     </div>      <!-- Javascript code for event bubbling -->     <script>         let div1 = document.querySelector("#div1");         let div2 = document.querySelector("#div2");         let div3 = document.querySelector("#div3");          div1.addEventListener("click", function (event) {             alert("Component 1 event clicked");         });          div2.addEventListener("click", function (event) {             alert("Component 2 event clicked");         });          div3.addEventListener("click", function (event) {             alert("Component 3 event clicked");         });     </script> </body>  </html> 

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20240729225144/eventbubbling.mp4

Capturing

It is the opposite of bubbling. The event handler is first on its parent component and then on the component where it was actually wanted to fire that event handler. In short, it means that the event is first captured by the outermost element and propagated to the inner elements. It is also called trickle down.

Example: In this example, we will create three div components and each component will have an event handler. When we click on div component 3 event, it will give an alert as “component 1 event clicked” and then all the way down  “component 2 event clicked” and then “component 3 event clicked” alerts will display. This is how event capturing or trickling happens from the “top to bottom”.

HTML
<!DOCTYPE html> <html>  <head>     <style>         #div1 {             background-color: lightgreen;             padding: 24px;             border: 1px solid black;         }          #div2 {             background-color: yellow;             padding: 18px;             border: 1px solid black;          }          #div3 {             background-color: orange;             border: 1px solid black;         }     </style> </head>  <body>     <h1 style="color: green">GeeksForGeeks</h1>     <h3>What is Event propagation, capturing, bubbling?</h3>      <div id="div1">         Component 1         <div id="div2">             component 2             <div id="div3">                 component 3             </div>         </div>     </div>      <!-- Javascript code for event capturing -->     <script>         let div1 = document.querySelector("#div1");         let div2 = document.querySelector("#div2");         let div3 = document.querySelector("#div3");          div1.addEventListener("click", function (event) {             alert("Component 1 event clicked");         }, true);          div2.addEventListener("click", function (event) {             alert("Component 2 event clicked");         }, true);          div3.addEventListener("click", function (event) {             alert("Component 3 event clicked");         }, true);     </script> </body>  </html> 

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20240729225144/eventcapturing.mp4


Next Article
What is the Event Driven Programming Paradigm ?
author
devendrasalunke
Improve
Article Tags :
  • Geeks Premier League
  • JavaScript
  • Web Technologies
  • Geeks-Premier-League-2022
  • JavaScript-Questions

Similar Reads

  • What is Event Bubbling and Capturing in JavaScript ?
    Events are a fundamental construct of the modern web. They are a special set of objects that allow for signaling that something has occurred within a website. By defining Event listeners, developers can run specific code as the events happen. This allows for implementing complex control logic on a w
    5 min read
  • What is Event bubbling and Event Capturing in JavaScript ?
    Event bubbling and event capturing are the two interesting concepts of JavaScript. Before diving deep into these fascinating concepts, let us first know about what an event listener is? An event listener is basically a function that waits for an event to occur. That event can be anything like a mous
    6 min read
  • Event Bubbling in JavaScript
    Event bubbling in JavaScript is a mechanism where an event triggered on a child element propagates upward through its ancestors in the DOM. It allows parent elements to respond to events triggered by their child elements. Propagation Direction: In event bubbling, the event starts at the target eleme
    6 min read
  • What is the Event Driven Programming Paradigm ?
    Event-driven programming is a paradigm where the execution of a program is determined by events such as user actions or messages. Programs respond to events with predefined actions, allowing for asynchronous and responsive behavior, often seen in GUI applications and distributed systems. Advantages
    4 min read
  • Spring - Application Events
    Spring is a popular Java-based framework that helps developers create enterprise applications quickly and efficiently. One of the most powerful features of the Spring framework is its Application Events feature, which allows developers to create custom event-driven applications. What are Spring Appl
    5 min read
  • What is Real Time Processing in Data Ingestion?
    The ability to handle data as it is generated has become increasingly important. Real-time data handling stands out as a strong method that allows instant decision-making, business efficiency, and improved user experiences. In this article, we looks into the idea, uses, methods, design, benefits, ob
    6 min read
  • What is An Event Loop in JavaScript?
    The event loop is an important concept in JavaScript that enables asynchronous programming by handling tasks efficiently. Since JavaScript is single-threaded, it uses the event loop to manage the execution of multiple tasks without blocking the main thread. [GFGTABS] JavaScript console.log("Sta
    4 min read
  • Web API Pointer Events
    Web API Pointer events are DOM events that are designed to create a single DOM event model to handle pointing input devices such as a pen, mouse, or touch screen. These are fired as a pointing device. It helps different devices work together smoothly, making sure your experience on websites is alway
    5 min read
  • HTML | bubbles Event Property
    The bubbles event property is used for returning a Boolean value which indicates whether an event is a bubbling event or not. The event is triggered if an event handler is set for that object, if not so then the event bubbles up to the object's parent. The event bubble up until it reaches the docume
    2 min read
  • What are JavaScript Events ?
    JavaScript Events are the action that happens due to the interaction of the user through the browser with the help of any input field, button, or any other interactive element present in the browser. Events help us to create more dynamic and interactive web pages. Also, these events can be used by t
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences