Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is event bubbling and capturing?

What is the difference between event bubbling and capturing? When should one use bubbling vs capturing?

like image 764
Arun P Johny Avatar asked Jan 06 '11 15:01

Arun P Johny


People also ask

What is event capture?

Event capturing is the event starts from top element to the target element. It is the opposite of Event bubbling, which starts from target element to the top element.

What is event bubbling and how is it different from event capturing?

Event Bubbling − Whenever an event happens on an element, the event handlers will first run on it and then on its parent and finally all the way up to its other ancestors. Event Capturing − It is the reverse of the event bubbling and here the event starts from the parent element and then to its child element.

What is event capturing with example?

Event capturing is one of two ways to do event propagation in the HTML DOM. In event capturing, an event propagates from the outermost element to the target element. It is the opposite of event bubbling, where events propagate outwards from the target to the outer elements. Capturing happens before bubbling.

What is events and explain event bubbling with an example?

Event bubbling directs an event to its intended target, and works like this: When an object (like a button) is clicked, an event is directed to the object. If an event handler is set for the object, the event handler is triggered. Then the event bubbles up (like a bubble in water) to the objects parent.


2 Answers

Event bubbling and capturing are two ways of event propagation in the HTML DOM API, when an event occurs in an element inside another element, and both elements have registered a handle for that event. The event propagation mode determines in which order the elements receive the event.

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.

Capturing is also called "trickling", which helps remember the propagation order:

trickle down, bubble up

Back in the old days, Netscape advocated event capturing, while Microsoft promoted event bubbling. Both are part of the W3C Document Object Model Events standard (2000).

IE < 9 uses only event bubbling, whereas IE9+ and all major browsers support both. On the other hand, the performance of event bubbling may be slightly lower for complex DOMs.

We can use the addEventListener(type, listener, useCapture) to register event handlers for in either bubbling (default) or capturing mode. To use the capturing model pass the third argument as true.

Example

<div>     <ul>         <li></li>     </ul> </div> 

In the structure above, assume that a click event occurred in the li element.

In capturing model, the event will be handled by the div first (click event handlers in the div will fire first), then in the ul, then at the last in the target element, li.

In the bubbling model, the opposite will happen: the event will be first handled by the li, then by the ul, and at last by the div element.

For more information, see

  • Event Order on QuirksMode
  • addEventListener on MDN
  • Events Advanced on QuirksMode

In the example below, if you click on any of the highlighted elements, you can see that the capturing phase of the event propagation flow occurs first, followed by the bubbling phase.

var logElement = document.getElementById('log');    function log(msg) {      logElement.innerHTML += ('<p>' + msg + '</p>');  }    function capture() {      log('capture: ' + this.firstChild.nodeValue.trim());  }    function bubble() {      log('bubble: ' + this.firstChild.nodeValue.trim());  }    function clearOutput() {      logElement.innerHTML = "";  }    var divs = document.getElementsByTagName('div');  for (var i = 0; i < divs.length; i++) {      divs[i].addEventListener('click', capture, true);      divs[i].addEventListener('click', bubble, false);  }  var clearButton = document.getElementById('clear');  clearButton.addEventListener('click', clearOutput);
p {      line-height: 0;  }    div {      display:inline-block;      padding: 5px;        background: #fff;      border: 1px solid #aaa;      cursor: pointer;  }    div:hover {      border: 1px solid #faa;      background: #fdd;  }
<div>1      <div>2          <div>3              <div>4                  <div>5</div>              </div>          </div>      </div>  </div>  <button id="clear">clear output</button>  <section id="log"></section>

Another example at JSFiddle.

like image 133
Arun P Johny Avatar answered Oct 07 '22 13:10

Arun P Johny


Description:

quirksmode.org has a nice description of this. In a nutshell (copied from quirksmode):

Event capturing

When you use event capturing

               | | ---------------| |----------------- | element1     | |                | |   -----------| |-----------     | |   |element2  \ /          |     | |   -------------------------     | |        Event CAPTURING          | ----------------------------------- 

the event handler of element1 fires first, the event handler of element2 fires last.

Event bubbling

When you use event bubbling

               / \ ---------------| |----------------- | element1     | |                | |   -----------| |-----------     | |   |element2  | |          |     | |   -------------------------     | |        Event BUBBLING           | ----------------------------------- 

the event handler of element2 fires first, the event handler of element1 fires last.


What to use?

It depends on what you want to do. There is no better. The difference is the order of the execution of the event handlers. Most of the time it will be fine to fire event handlers in the bubbling phase but it can also be necessary to fire them earlier.

like image 34
Felix Kling Avatar answered Oct 07 '22 14:10

Felix Kling