Answers to your questions can be found in the event.stopPropagation() docs.
With regards to live('click') versus 'click' and bubbling:
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 always propagate to the element to which they are delegated; event handlers on any elements below it will already have been executed by the time the delegated event handler is called.
So what you are seeing is expected behavior. live('click') won't stop bubbling. The same is true of the now-preferred .delegate().
This answer describes extensively the problems of stopping propegation when using .live. See also the .live() documention:
The event bubbles up until it reaches the root of the tree, which is where .live() binds its special handlers by default.
- As of jQuery 1.4, event bubbling can optionally stop at a DOM element "context".
With regards to stopPropagation itself:
Kill the bubbling on the click event.
So stopPropagation() should prevent bubbling wherever return false; will, although not where it won't. It is preventDefault() that will not prevent bubbling. return false;
is effectively stopPropagation() and preventDefault().
In response to each jsfiddle example.
1) http://jsfiddle.net/2zsLy/10/
return false;
will not prevent events bound using live
or delegate
from bubbling up. The documentation explicitly says this about all bubbling, whether done with return false;
or with stopPropagation()
. So what happens is
- Click on
<p>
- Click bubbles to
<body>
, where body.click()
is triggered
- Click bubbles to document root, where it matches
$(event.target).closest('p');
and so live('click')
is invoked
- Return false happens, but body handler was invoked at step 2
2) http://jsfiddle.net/2zsLy/11/
In this case the event gets to the same spot (document root) and the return false stops the propagation to the further event bound via live
, which is on the body.
- Click on
<p>
- Click bubbles to
<body>
, but since body's handler is attached via live it is not invoked yet
- Click bubbles to document root, where it matches
$(event.target).closest('p');
and so live('click')
is invoked
- Return false happens
live('click')
is not invoked on the body due to the return false occurring.
3) http://jsfiddle.net/2zsLy/13/
stopPropagation()
is explicitly used to prevent bubbling, although it has the same restrictions return false;
does - so it won't prevent it in the case of a live('click')
event if there are click handlers bound via bind
.