
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain the event flow process in Javascript
In the JavaScript, Event Flow process is completed by three concepts −
Event Target − The actual DOM object on which the event occured.
Event Bubbling − Explained below
Event Capturing − Explained below
Event bubbling is the order in which event handlers are called when one element is nested inside a second element, and both elements have registered a listener for the same event (a click, for example). With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements.
With capturing, the event is first captured by the outermost element and propagated to the inner elements.
Let's look at examples of both.
For both of the following examples, create the following HTML −
Example
<div id='outer' style='background-color:red;display:inline-block;padding:50px;'> Outer Div <div id='inner' style='background-color:yellow;display:inline-block;padding:50px;'> Inner Div </div> </div>
Event Bubbling
document.querySelector('#outer').addEventListener('click', e => { console.log('Outer div is clicked'); }, false); document.querySelector('#inner').addEventListener('click', e => { console.log('Inner div is clicked'); }, false);
If you run the above code and click in the inner div, you'll get the log −
Inner div is clicked
Outer div is clicked
Event Capturing
document.querySelector('#outer').addEventListener('click', e => { console.log('Outer div is clicked'); }, true); document.querySelector('#inner').addEventListener('click', e => { console.log('Inner div is clicked'); }, true);
Output
If you run the above code and click in the inner div, you'll get the log −
Outer div is clicked Inner div is clicked
- Related Questions & Answers
- Explain the concatenation process in DFA
- Explain the complementation process in DFA
- Node.js – Process beforeExit Event
- Node.js – Process Warning Event
- Explain Union process in DFA
- Explain the process of mergers & acquisition
- Explain the cross product method process in DFA
- Node.js – process ‘exit’ Event
- Explain free cash flow to firm (FCFF)
- Explain the performance ratios by using cash flow from operations
- Explain the intersection process of two DFA’s
- Explain discounted cash flow analysis in merger and acquisitions
- Event bubbling vs event capturing in JavaScript?
- Explain Agile Software Process and its Principles
- How to use labels to control the Flow in JavaScript?