Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the bubbling concept?

I've heard of events and SO answers "bubbling up", but what has all that got to do with bubbles?

like image 419
Robin Rodricks Avatar asked Aug 31 '09 03:08

Robin Rodricks


People also ask

What is event bubbling example?

Event bubbling is a method of event propagation in the HTML DOM API when an event is in an element inside another element, and both elements have registered a handle to that event. It is a process that starts with the element that triggered the event and then bubbles up to the containing elements in the hierarchy.

How does event bubbling work?

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 or document object (Provided the handler is initialized).

What is bubbling and capturing and what is the difference?

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.

What happens first capturing or bubbling?

Event capturing means propagation of event is done from ancestor elements to child element in the DOM while event bubbling means propagation is done from child element to ancestor elements in the DOM. The event capturing occurs followed by event bubbling.


2 Answers

Event bubbling is the idea of information moving up through a deep structure, when proper design dictates that normally information should only flow downward.

In very basic terms, think of a single object. Properly designed, this object should only know about its own child objects. It should have no direct interaction with its parent. Its children and parent objects, in turn, should follow the same rules. In effect, this means that information can only flow downward - a parent can invoke a method, send data into or extract data from its child, but the child cannot forcibly do the same to its parent.

Think of what happens when you blow an air bubble underwater - you don't have to push the air towards the surface - you simply release it and it moves on its own. The same concept applies to event bubbling - deeper controls simply "release" their information - usually via an event - and it "floats" up the chain without directly invoking anything.

With regard to a website like Stackoverflow (or practically any kind of site), the concept of bubbling is the same. Obviously each individual post ought not directly put itself on the home page, but when a single post has been updated, that event is released from the deepest point in the hierarchy - a single post - and floats up to eventually the top level, where it is dealt with (choosing whether or not to display on the home page).

like image 76
Rex M Avatar answered Oct 24 '22 20:10

Rex M


If you are asking about the term, I guess it is an analogy to an event 'bubbling' up to the top, like an air bubble does in liquid.

If you are asking what event bubbling is it is an event that is caught by one object that will refire it to any other objects that are listening to it.

To quote a good article here

... a technique called event bubbling that allows a child control to propagate events up its containment hierarchy. Event bubbling enables events to be raised from a more convenient location in the controls hierarchy and allows event handlers to be attached to the original control as well as to the control that exposes the bubbled event.

like image 24
johnc Avatar answered Oct 24 '22 22:10

johnc