|
135 | 135 | | 85 | [What is NaN property](#what-is-nan-property) |
|
136 | 136 | | 86 | [What is the purpose of isFinite function](#what-is-the-purpose-of-isfinite-function) |
|
137 | 137 | | 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) | |
140 | 140 | | 90 | [How do you submit a form using JavaScript](#how-do-you-submit-a-form-using-javascript) |
|
141 | 141 | | 91 | [How do you find operating system details](#how-do-you-find-operating-system-details) |
|
142 | 142 | | 92 | [What is the difference between document load and DOMContentLoaded events](#what-is-the-difference-between-document-load-and-domcontentloaded-events) |
|
|
2291 | 2291 |
|
2292 | 2292 | 87. ### What is an event flow
|
2293 | 2293 |
|
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. |
2295 | 2295 |
|
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. |
2297 | 2297 |
|
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. |
2300 | 2303 |
|
2301 | 2304 | **[⬆ Back to Top](#table-of-contents)**
|
2302 | 2305 |
|
2303 |
| -88. ### What is event bubbling |
| 2306 | +88. ### What is event capturing |
2304 | 2307 |
|
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. |
2306 | 2309 |
|
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. |
2308 | 2311 |
|
2309 |
| - ```javascript |
2310 |
| - <div> |
2311 |
| - <button class="child">Hello</button> |
2312 |
| - </div> |
| 2312 | + ```javascript |
| 2313 | + <div> |
| 2314 | + <button class="child">Hello</button> |
| 2315 | + </div> |
2313 | 2316 |
|
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"); |
2317 | 2320 |
|
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 |
2323 | 2325 |
|
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 | + ``` |
2331 | 2334 |
|
2332 | 2335 | **[⬆ Back to Top](#table-of-contents)**
|
2333 | 2336 |
|
2334 |
| -89. ### What is event capturing |
| 2337 | +89. ### What is event bubbling |
2335 | 2338 |
|
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. |
2337 | 2340 |
|
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. |
2339 | 2342 |
|
2340 | 2343 | ```javascript
|
2341 | 2344 | <div>
|
|
2346 | 2349 | const parent = document.querySelector("div");
|
2347 | 2350 | const child = document.querySelector(".child");
|
2348 | 2351 |
|
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 | + }); |
2355 | 2356 |
|
2356 | 2357 | child.addEventListener("click", function () {
|
2357 | 2358 | console.log("Child");
|
2358 | 2359 | });
|
2359 | 2360 | </script>
|
2360 |
| - // Parent |
2361 |
| - // Child |
| 2361 | + //Child |
| 2362 | + //Parent |
2362 | 2363 | ```
|
2363 | 2364 |
|
| 2365 | + Here, at first, the event triggers on the child button. Thereafter it bubbles up and triggers the parent div's event handler. |
| 2366 | + |
2364 | 2367 | **[⬆ Back to Top](#table-of-contents)**
|
2365 | 2368 |
|
| 2369 | +
|
2366 | 2370 | 90. ### How do you submit a form using JavaScript
|
2367 | 2371 |
|
2368 | 2372 | 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