Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event bubbling

I want to understand how exactly to interpret bubbling. Does it mean going up the HTML code hierarchy or something else?

Secondly, I was going through an example and I could not understand the last part where it says

The P-based click handler listens for the click event and then prevents it from being propagated (bubbling up)

What does this mean?

like image 377
copenndthagen Avatar asked Aug 04 '11 17:08

copenndthagen


People also ask

What is event bubbling in jQuery?

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.

Which jQuery event function can take advantage of event bubbling?

The concept of "bubbling up" is like if you have a child element with a click event and you don't want it to trigger the click event of the parent. You could use event. stopPropagation() . event.

How do I stop jQuery from bubbling?

The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed. Tip: Use the event. isPropagationStopped() method to check whether this method was called for the event.

How do you do event bubbling?

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.


2 Answers

The concept of "bubbling up" is like if you have a child element with a click event and you don't want it to trigger the click event of the parent. You could use event.stopPropagation().

event.stopPropagation() basically says only apply this click event to THIS CHILD NODE and don't tell the parent containers anything because I don't want them to react.

Event Capturing:

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

Event Bubbling:

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

If you are using live() or delegate() you will need to return false;, though it may not work. Read the quote below.

Per jQuery docs:

Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.

In the past it was a platform issue, Internet Explorer had a bubbling model, and Netscape was more about capturing (yet supported both).

The W3C model calls for you be able to choose which one you want.

I think bubbling is more popular because, as stated there are some platforms that only support bubbling...and it sort of makes sense as a "default" mode.

Which one you choose is largely a product of what you are doing and what makes sense to you.

More info http://www.quirksmode.org/js/events_order.html

Another great resource: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

like image 112
AlienWebguy Avatar answered Oct 08 '22 17:10

AlienWebguy


return false;

will prevent "bubbling". It's used to stop default actions like checking a checkbox, opening a select, a click, etc.

To stop further handlers from executing after one bound using .live(), the handler must return false. Calling .stopPropagation() will not accomplish this.

From Caveats in jQuery .live()


Reasoning (thanks to @AlienWebguy):

The reason stopPropagation() doesn't work with live() is that live() binds the event to document so by the time it fires there's no where else for it to propagate.

like image 44
Joe Avatar answered Oct 08 '22 15:10

Joe