In addition to T.J. Crowders answer, I have added some more handlers - including the newer .on(...)
handler to the snippet so you can see which events are being hidden and which ones not.
What I also found is that .live()
is not only deprecated, but was deleted since jQuery 1.9.x. But the other ones, i.e.
.click
, .delegate
/.undelegate
and .on
/.off
are still there.
Also note there is more discussion about this topic here on Stackoverflow.
If you need to fix legacy code that is relying on .live, but you require to use a new version of jQuery (> 1.8.3), you can fix it with this snippet:
// fix if legacy code uses .live, but you want to user newer jQuery library
if (!$.fn.live) {
// in this case .live does not exist, emulate .live by calling .on
$.fn.live = function(events, handler) {
$(this).on(events, null, {}, handler);
};
}
The intention of the snippet below, which is an extension of T.J.'s script, is that you can try out by yourself instantly what happens if you bind multiple handlers - so please run the snippet and click on the texts below:
jQuery(function($) {
// .live connects function with all spans
$('span').live('click', function() {
display("<tt>live</tt> caught a click!");
});
// --- catcher1 events ---
// .click connects function with id='catcher1'
$('#catcher1').click(function() {
display("Click Catcher1 caught a click and prevented <tt>live</tt> from seeing it.");
return false;
});
// --- catcher2 events ---
// .click connects function with id='catcher2'
$('#catcher2').click(function() {
display("Click Catcher2 caught a click and prevented <tt>live</tt>, <tt>delegate</tt> and <tt>on</tt> from seeing it.");
return false;
});
// .delegate connects function with id='catcher2'
$(document).delegate('#catcher2', 'click', function() {
display("Delegate Catcher2 caught a click and prevented <tt>live</tt> from seeing it.");
return false;
});
// .on connects function with id='catcher2'
$(document).on('click', '#catcher2', {}, function() {
display("On Catcher2 caught a click and prevented <tt>live</tt> from seeing it.");
return false;
});
// --- catcher3 events ---
// .delegate connects function with id='catcher3'
$(document).delegate('#catcher3', 'click', function() {
display("Delegate Catcher3 caught a click and <tt>live</tt> and <tt>on</tt> can see it.");
return false;
});
// .on connects function with id='catcher3'
$(document).on('click', '#catcher3', {}, function() {
display("On Catcher3 caught a click and and <tt>live</tt> and <tt>delegate</tt> can see it.");
return false;
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
<!-- with JQuery 1.8.3 it still works, but .live was removed since 1.9.0 -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<style>
span.frame {
line-height: 170%; border-style: groove;
}
</style>
<div>
<span class="frame">Click me</span>
<span class="frame">or me</span>
<span class="frame">or me</span>
<div>
<span class="frame">I'm two levels in</span>
<span class="frame">so am I</span>
</div>
<div id='catcher1'>
<span class="frame">#1 - I'm two levels in AND my parent interferes with <tt>live</tt></span>
<span class="frame">me too</span>
</div>
<div id='catcher2'>
<span class="frame">#2 - I'm two levels in AND my parent interferes with <tt>live</tt></span>
<span class="frame">me too</span>
</div>
<div id='catcher3'>
<span class="frame">#3 - I'm two levels in AND my parent interferes with <tt>live</tt></span>
<span class="frame">me too</span>
</div>
</div>