Skip to content

Commit 1d9cd77

Browse files
committed
Improve event flow questions
1 parent 1d3d096 commit 1d9cd77

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

README.md

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@
135135
| 85 | [What is NaN property](#what-is-nan-property) |
136136
| 86 | [What is the purpose of isFinite function](#what-is-the-purpose-of-isfinite-function) |
137137
| 87 | [What is an event flow](#what-is-an-event-flow) |
138-
| 88 | [What is event bubbling](#what-is-event-bubbling) |
139-
| 89 | [What is event capturing](#what-is-event-capturing) |
138+
| 88 | [What is event capturing](#what-is-event-capturing) |
139+
| 89 | [What is event bubbling](#what-is-event-bubbling) |
140140
| 90 | [How do you submit a form using JavaScript](#how-do-you-submit-a-form-using-javascript) |
141141
| 91 | [How do you find operating system details](#how-do-you-find-operating-system-details) |
142142
| 92 | [What is the difference between document load and DOMContentLoaded events](#what-is-the-difference-between-document-load-and-domcontentloaded-events) |
@@ -2291,51 +2291,54 @@
22912291

22922292
87. ### What is an event flow
22932293

2294-
Event flow is the order in which event is received on the web page. When you click an element that is nested in various other elements, before your click actually reaches its destination, or target element, it must trigger the click event for each of its parent elements first, starting at the top with the global window object.
2294+
Event flow refers to the order in which events are handled in the browser when a user interacts with elements on a webpage like clicking, typing, hovering, etc.
22952295

2296-
There are two ways of event flow
2296+
When you click an element that is nested in various other elements, before your click actually reaches its destination, or target element, it must trigger the click event for each of its parent elements first, starting at the top with the global window object.
22972297

2298-
1. Top to Bottom(Event Capturing)
2299-
2. Bottom to Top (Event Bubbling)
2298+
Hence, there are three phases in JavaScript’s event flow:
2299+
2300+
1. Event Capturing(Top to Bottom): The event starts from the window/document and moves down the DOM tree toward the target element.
2301+
2. Target phase: The event reaches the target element — the element that was actually interacted with.
2302+
3. Event Bubbling(Bottom to Top): The event then bubbles back up from the target element to the root.
23002303

23012304
**[⬆ Back to Top](#table-of-contents)**
23022305

2303-
88. ### What is event bubbling
2306+
88. ### What is event capturing
23042307

2305-
Event bubbling is a type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element in the same nesting hierarchy till it reaches the outermost DOM element(i.e, global window object).
2308+
Event capturing is a phase of event propagation in which an event is first intercepted by the outermost ancestor element, then travels downward through the DOM hierarchy until it reaches the target (innermost) element.
23062309

2307-
By default, event handlers triggered in event bubbling phase as shown below,
2310+
To handle events during the capturing phase, you need to pass `true` as the third argument to the `addEventListener` method.
23082311

2309-
```javascript
2310-
<div>
2311-
<button class="child">Hello</button>
2312-
</div>
2312+
```javascript
2313+
<div>
2314+
<button class="child">Hello</button>
2315+
</div>
23132316

2314-
<script>
2315-
const parent = document.querySelector("div");
2316-
const child = document.querySelector(".child");
2317+
<script>
2318+
const parent = document.querySelector("div");
2319+
const child = document.querySelector(".child");
23172320

2318-
parent.addEventListener("click",
2319-
function () {
2320-
console.log("Parent");
2321-
}
2322-
);
2321+
// Capturing phase: parent listener (runs first)
2322+
parent.addEventListener("click", function () {
2323+
console.log("Parent (capturing)");
2324+
}, true); // `true` enables capturing
23232325

2324-
child.addEventListener("click", function () {
2325-
console.log("Child");
2326-
});
2327-
</script>
2328-
// Child
2329-
// Parent
2330-
```
2326+
// Bubbling phase: child listener (runs after)
2327+
child.addEventListener("click", function () {
2328+
console.log("Child (bubbling)");
2329+
});
2330+
</script>
2331+
// Parent (capturing)
2332+
// Child (bubbling)
2333+
```
23312334
23322335
**[⬆ Back to Top](#table-of-contents)**
23332336
2334-
89. ### What is event capturing
2337+
89. ### What is event bubbling
23352338
2336-
Event capturing is a type of event propagation where the event is first captured by the outermost element, and then successively triggers on the descendants (children) of the target element in the same nesting hierarchy till it reaches the innermost target DOM element.
2339+
Event bubbling is a type of event propagation in which an event first triggers on the innermost target element (the one the user interacted with), and then bubbles up through its ancestors in the DOM hierarchy — eventually reaching the outermost elements, like the document or window.
23372340
2338-
You need to pass `true` value for `addEventListener` method to trigger event handlers in event capturing phase.
2341+
By default, event listeners in JavaScript are triggered during the bubbling phase, unless specified otherwise.
23392342
23402343
```javascript
23412344
<div>
@@ -2346,23 +2349,24 @@
23462349
const parent = document.querySelector("div");
23472350
const child = document.querySelector(".child");
23482351

2349-
parent.addEventListener("click",
2350-
function () {
2351-
console.log("Parent");
2352-
},
2353-
true
2354-
);
2352+
// Bubbling phase (default)
2353+
parent.addEventListener("click", function () {
2354+
console.log("Parent");
2355+
});
23552356

23562357
child.addEventListener("click", function () {
23572358
console.log("Child");
23582359
});
23592360
</script>
2360-
// Parent
2361-
// Child
2361+
//Child
2362+
//Parent
23622363
```
23632364
2365+
Here, at first, the event triggers on the child button. Thereafter it bubbles up and triggers the parent div's event handler.
2366+
23642367
**[⬆ Back to Top](#table-of-contents)**
23652368
2369+
23662370
90. ### How do you submit a form using JavaScript
23672371
23682372
You can submit a form using `document.forms[0].submit()`. All the form input's information is submitted using onsubmit event handler

0 commit comments

Comments
 (0)