Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Event Bubbling and Event Capturing in Javascript Explained: How to Utilize?

Updated on 11 November, 2024

6.55K+ views
7 min read

Introduction 

Two very common terminologies used for event flow in JavaScript are Event Bubbling and Event Capturing.

These are two important ways in which an event is propagated in the HTML DOM API when both elements register a handle for an event and the event occurs in an element nested in another element. The order in which an event is received by the elements is determined by the mode of event propagation. Read on to know the use of Event Bubbling and Event Capturing in JavaScript and why these are required in web page development. 

Check out our free courses to get an edge over the competition.

Key takeaways

  • Explaining the terms event, event flow, event handlers and event listeners
  • Event Bubbling and Event Capturing  in JavaScript 
  • Utility of Event Bubbling and Event Capturing in JavaScript

Events and event flow 

The interaction of HTML web pages with JavaScript is mediated by events that occur when the page is manipulated by the user or the browser. The loading of a page, the user clicking a button, closing a window, are all examples of events.

The order in which the events are received on the web page is termed as event flow. The process of event flow is completed in 3 steps – event capturing (intercepting the event), event targeting (target gets the event) and event bubbling (response to the event).

Check out upGrad’s Advanced Certification in Cyber Security 

Event Handlers and Event Listeners in JavaScript

Event Handler is a JavaScript code that is written inside the HTML tags instead of inside the <script> tags. Event Handlers execute JavaScript when an event happens such as pressing a button. The basic syntax is: name_of_handler=”JavaScript code here”

<a href=”https://www.google.com” onClick=”alert(‘hello!”)”>Goolge</a>

A procedure that awaits the occurrence of an event is an Event Listener. JavaScript has an in-built function, the addEventListener(), that receives the event to listen to, and calls a second argument when the said event is fired. The syntax is: element.addEventListener(event, listener);

Check out upGrad’s Advanced Certification in Cloud Computing 

Event Bubbling in JavaScript

Event Bubbling is a common terminology that is encountered while developing a web page or a web application using JavaScript. Event bubbling is a stage in the process of event flow when the event begins at the most specific element or the most deeply nested node in the DOM and subsequently flows upwards towards the node which is least specific, that is, the document. 

Source

<!DOCTYPE HTML>
<html>
<head>
<title>……</title>
</head>
<body>
<div id=”demo”> Press here.</div>
</body>
</html>

On clicking the element <div>, the event ‘click’ takes place in the following order:

  1. <div>
  2. <body>
  3. <html>
  4. Document

The event ‘click’ fires the element <div>. Each node is subsequently fired along the DOM tree, upwards, until the object document is reached.

Read: Full stack Project Ideas and Topics

Event Capturing in JavaScript

Event Capturing is an alternative model in event flow that was first introduced by Netscape Browser. As stated by this model, an event is received first by the least specific node and the most specific node or the most deeply nested element receives the event last. In this model, an event is intercepted before it reaches the actual target. However, unlike Event Bubbling, modern browsers lack support for this model and thus, Event Capturing can be used only in particular circumstances.

Source

Referring to the example stated in the previous section, clicking the element <div> fires the event ‘click’ in the following order:

  1. Document
  2. <body>
  3. <html>
  4. <div>

What happens in the Event Capturing Phase?

In the event capturing phase, the capturer listeners whose value is registered as “true”, are called. It is written in the following way:

el.addEventListener(‘Click’, listener, true)

Here an event is captured because the value of the listener is registered as “true”. In case there is no value, the default value is “false” with the outcome that the event does not get captured. Hence in this phase, only events with true value get called and are captured. In the subsequent target phase, all listeners that are registered are called, irrespective of whether their value is registered as true or false.

DOM event flow in JavaScript

The DOM Level 2 specifies three stages to the model of event flow:

  • Event Capturing Phase
  • At the target
  • Event Bubbling Phase

Source

If there is an opportunity of intercepting the event, Event Capturing occurs first. This is followed by the actual target getting the event. Eventually, at the Event Bubbling Phase, The final response to the event takes place. Referring to the example stated in the previous section, clicking the element <div> fires the event ‘click’ in the order illustrated in the diagram above.

Read: Full Stack Developer Salary in India

Utility of Event Bubbling and Event Capturing in JavaScript

In the Event Bubbling Phase, only the events having flag value “false” (non-capturers) are called. Event Bubbling and Event Capturing are important aspects of DOM. 

el.addEventListener(‘Click’, listener, false) // listener doesn’t capture
el.addEventListener(‘Click’, listener) // listener doesn’t capture

The above code exemplifies how the bubbling and the capturing phases work. Every event does not reach the target. Some events are not bubbled up and stop post the target phase. The bubbling event is not applicable in all kinds of events and the listener should have the Boolean property “.bubble” of the object of the event besides possessing some other properties such as – e.target (to reference the target of the event) and e.eventPhase (current listeners register on this mode).

Conclusion

The event flow in JavaScript has three important phases – Event Capturing phase, target phase and Event Bubbling Phase. Event Capturing is the first to occur, where the events are intercepted if necessary. This is followed by the event reaching the actual target and the final phase is bubbling, when the ultimate response to an event takes place. Thus, it can be appropriately concluded that Event Bubbling and Event Capturing in JavaScript are the foundations on which event handler and event delegation are dependent.

If you’re interested to learn more about full stack, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. How is Javascript different from Java?

The primary difference between Java and Javascript is that Java is an Object-Oriented Programming language, while JavaScript is an Object-Oriented Programming (OOP) scripting language. Java is used to develop apps that operate on a virtual computer or in a web browser. Developers utilize Javascript, which is only capable of running in a browser. JavaScript code is simply text and does not need to be compiled. JavaScript is an object-oriented, lightweight interpreted programming language that lets you add dynamism to somewhat static HTML websites. Opera, Mozilla Firefox, and other web browsers support the language's overall core.

2. What are the advantages of Javascript?

There are many advantages of Javascript. Firstly, JavaScript is always run on the client to conserve bandwidth and speed up the execution process, regardless of where it is hosted. JavaScript is particularly fast since it can be run directly in the client-side browser. Unless external resources are required, network requests to a backend server have no effect on JavaScript. It is the scripting language that can be used in a variety of applications and works well with other languages. The order to sustain all contemporary browsers and generate identical results is JavaScript's significant benefit. JavaScript is used all across the internet.

3. What are the disadvantages of Javascript?

Like any other language, Javascript has its own set of drawbacks. Large applications written in Javascript may be challenging to create. This is especially true for more significant front-end projects. The quantity of tools that must be figured together to create an environment for such a project makes setup a lengthy procedure. The biggest issue or drawback with JavaScript is that the code is constantly exposed to everyone who has access to the internet. Regardless of how fast JavaScript interprets, the JavaScript Document Object Model (DOM) is sluggish and can never render as quickly as HTML. If a JavaScript problem occurs, the entire website may stop working. JavaScript mistakes are tolerated quite well by browsers. Various browsers interpret JavaScript in different ways. This makes cross-browser programming a little more challenging to read and develop.

RELATED PROGRAMS