Description: Attach an event handler for all elements which match the current selector, now and in the future.
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page. See the discussion of direct versus delegated events in the .on() method for more information.
Rewriting the .live() method in terms of its successors is straightforward; these are templates for equivalent calls for all three event attachment methods:
$( selector ).live( events, data, handler ); // jQuery 1.3+
$( document ).delegate( selector, events, data, handler ); // jQuery 1.4.3+
$( document ).on( events, selector, data, handler ); // jQuery 1.7+
The events argument can either be a space-separated list of event type names and optional namespaces, or an object of event name strings and handlers. The data argument is optional and can be omitted. For example, the following three method calls are functionally equivalent (but see below for more effective and performant ways to attach delegated event handlers):
$( "a.offsite" ).live( "click", function() {
alert( "Goodbye!" ); // jQuery 1.3+
});
$( document ).delegate( "a.offsite", "click", function() {
alert( "Goodbye!" ); // jQuery 1.4.3+
});
$( document ).on( "click", "a.offsite", function() {
alert( "Goodbye!" ); // jQuery 1.7+
});
Use of the .live() method is no longer recommended since later versions of jQuery offer better methods that do not have its drawbacks. In particular, the following issues arise with the use of .live():
.live() method, which may be time-consuming on large documents.$( "a" ).find( ".offsite, .external" ).live( ... ); is not valid and does not work as expected..live() events are attached at the document element, events take the longest and slowest possible path before they are handled.click event does not bubble to the document body for most elements and cannot be used with .live() without applying one of the following workarounds:
a or button, as both of these do bubble to document..on() or .delegate() attached to an element below the level of document.body, since mobile iOS does bubble within the body.cursor:pointer to the element that needs to bubble clicks (or a parent including document.documentElement). Note however, this will disable copy\paste on the element and cause it to be highlighted when touched.event.stopPropagation() in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to document..live() method interacts with other event methods in ways that can be surprising, e.g., $( document ).off( "click" ) removes all click handlers attached by any call to .live()!For pages still using .live(), this list of version-specific differences may be helpful:
.live(), the handler must return false. Calling .stopPropagation() will not accomplish this..live() method supports custom events as well as all JavaScript events that bubble. It also supports certain events that don't bubble, including change, submit, focus and blur.click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.
$( "a" ).live( "click", function() {
return false;
});
$( "a" ).live( "click", function( event ) {
event.preventDefault();
});
$( "p" ).live( "myCustomEvent", function( event, myName, myValue ) {
$( this ).text( "Hi there!" );
$( "span" )
.stop()
.css( "opacity", 1 )
.text( "myName = " + myName )
.fadeIn( 30 )
.fadeOut( 1000 );
});
$( "button" ).click(function() {
$( "p" ).trigger( "myCustomEvent" );
});
$( "p" ).live({
click: function() {
$( this ).after( "<p>Another paragraph!</p>" );
},
mouseover: function() {
$( this ).addClass( "over" );
},
mouseout: function() {
$( this ).removeClass( "over" );
}
});