Event Propagation, Bubbling and Capturing in JavaScript

Event propagation in JavaScript refers to the way in which events bubble up from the specific element on which they are triggered to its parent elements. This allows us to add event listeners to parent elements in order to handle events triggered on their child elements.

For example, if a user clicks on a button inside a form element, the click event will first be triggered on the button, then on the form, and finally on the document. This allows us to add event listeners to the form and the document in order to handle the event and perform actions based on the element that was clicked.

Event propagation can be stopped using the stopPropagation() method, which prevents the event from bubbling up to its parent elements. This can be useful in situations where we only want to handle the event on the specific element on which it was triggered, and not on any of its parent elements.

In JavaScript, event bubbling and capture refer to the way in which events are propagated from the specific element on which they are triggered to its parent elements.

When an event is triggered on an element, it first runs the event listeners on that element (this is called the “capture” phase), then it bubbles up to its parent elements and runs the event listeners on those elements (this is called the “bubbling” phase).

Here is an example of using event bubbling and capture in JavaScript:

// Select the button element on the page
var button = document.querySelector('button');

// Add a click event listener to the button
button.addEventListener('click', function(event) {
  // This event listener will be triggered during the capture phase
  console.log('Button click (capture phase)');
}, true);

// Select the form element on the page
var form = document.querySelector('form');

// Add a click event listener to the form
form.addEventListener('click', function(event) {
  // This event listener will be triggered during the bubbling phase
  console.log('Form click (bubbling phase)');
});

// Select the document element
var document = document.querySelector('document');

// Add a click event listener to the document
document.addEventListener('click', function(event) {
  // This event listener will be triggered during the bubbling phase
  console.log('Document click (bubbling phase)');
});

If a user clicks on a button inside a form element, the click event will first be triggered on the button (capture phase), then it will bubble up to the form element and run the event listeners on the form (bubbling phase), and finally it will bubble up to the document and run the event listeners on the document (bubbling phase).

We can control whether an event listener is triggered during the capture or bubbling phase by using the addEventListener() method and specifying the useCapture parameter. By default, event listeners are triggered during the bubbling phase. To trigger an event listener during the capture phase, we can set the useCapture parameter to true.

In this example, we are adding event listeners to the button, form, and document elements on the page.

The event listener on the button is triggered during the capture phase, the event listeners on the form and document are triggered during the bubbling phase.

This allows us to handle events on specific elements and their parent elements.

I hope this helps! Let me know if you have any questions.