jquery .live('click') vs .click()
There might be times when you explicitly want to only assign the click handler to objects which already exist, and handle new objects differently. But more commonly, live doesn't always work. It doesn't work with chained jQuery statements such as:
$(this).children().live('click',doSomething);
It needs a selector to work properly because of the way events bubble up the DOM tree.
Edit: Someone just upvoted this, so obviously people are still looking at it. I should point out that live
and bind
are both deprecated. You can perform both with .on()
, which IMO is a much clearer syntax. To replace bind
:
$(selector).on('click', function () {
...
});
and to replace live
:
$(document).on('click', selector, function () {
...
});
Instead of using $(document)
, you can use any jQuery object which contains all the elements you're monitoring the clicks on, but the corresponding element must exist when you call it.
(Note 29/08/2017: live
was deprecated many versions ago and removed in v1.9. delegate
was deprecated in v3.0. In both cases, use the delegating signature of on
instead [also covered below].)
live
happens by capturing the event when it's bubbled all the way up the DOM to the document root, and then looking at the source element. click
happens by capturing the event on the element itself. So if you're using live
, and one of the ancestor elements is hooking the event directly (and preventing it continuing to bubble), you'll never see the event on your element. Whereas normally, the element nearest the event (click or whatever) gets first grab at it, the mix of live
and non-live
events can change that in subtle ways.
For example:
jQuery(function($) {
$('span').live('click', function() {
display("<tt>live</tt> caught a click!");
});
$('#catcher').click(function() {
display("Catcher caught a click and prevented <tt>live</tt> from seeing it.");
return false;
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
<div>
<span>Click me</span>
<span>or me</span>
<span>or me</span>
<div>
<span>I'm two levels in</span>
<span>so am I</span>
</div>
<div id='catcher'>
<span>I'm two levels in AND my parent interferes with <tt>live</tt></span>
<span>me too</span>
</div>
</div>
<!-- Using an old version because `live` was removed in v1.9 -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
I'd recommend using delegate
over live
when you can, so you can more thoroughly control the scope; with delegate
, you control the root element that captures the bubbling event (e.g., live
is basically delegate
using the document root as the root). Also, recommend avoiding (where possible) having delegate
or live
interacting with non-delegated, non-live event handling.
Here several years later, you wouldn't use either live
or delegate
; you'd use the delegating signature of on
, but the concept is still the same: The event is hooked on the element you call on
on, but then fired only when descendants match the selector given after the event name:
jQuery(function($) {
$(document).on('click', 'span', function() {
display("<tt>live</tt> caught a click!");
});
$('#catcher').click(function() {
display("Catcher caught a click and prevented <tt>live</tt> from seeing it.");
return false;
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
<div>
<span>Click me</span>
<span>or me</span>
<span>or me</span>
<div>
<span>I'm two levels in</span>
<span>so am I</span>
</div>
<div id='catcher'>
<span>I'm two levels in AND my parent interferes with <tt>live</tt></span>
<span>me too</span>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>