groups

Link to this snippet:


Download to Code Collector

language: JS-JQuery
licence: Other

JQUERY : Aide sur live() (anglais)

options: send to code collectorview all pierre alexandre payet's snippets
Events/live

live( type, fn )

Added in jQuery 1.3: Binds a handler to an event (like click) for all current - and future - matched element. Can also bind custom events.
Possible event values: click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup 
Currently not supported: blur, focus, mouseenter, mouseleave, change, submit
Unlike .bind(), only a single event can be bound in each call to the .live() method.

This method works and behaves very similarly to jQuery's bind method but with a few important distinctions:

When you bind a "live" event it will bind to all current and future elements on the page (using event delegation). For example if you bound a live click to all "li" elements on the page then added another li at a later time - that click event would continue to work for the new element (this is not the case with bind which must be re-bound on all new elements).
Live events do not bubble in the traditional manner and cannot be stopped using stopPropagation or stopImmediatePropagation. For example, take the case of two click events - one bound to "li" and another "li a". Should a click occur on the inner anchor BOTH events will be triggered. This is because when a $("li").bind("click", fn); is bound you're actually saying "Whenever a click event occurs on an LI element - or inside an LI element - trigger this click event." To stop further processing for a live event, fn must return false. This also means, that if any of the parent nodes for the "li" has a click event handler that returns false, the click event will never reach the document node, and as a consequence the live event will never be triggered.
Live events currently only work when used against a selector. For example, this would work: $("li a").live(...) but this would not: $("a", someElement).live(...) and neither would this: $("a").parent().live(...).
.live() behaves similarly to the popular liveQuery plugin but with a few major differences:

.live (currently) supports a subset of all events. Note the full list of supported/not-supported events above.
.live doesn't support the no-event style callback that liveQuery provides. Only event handlers can be bound with .live.
.live doesn't have a "setup" or "cleanup" step, since all events are delegated rather than bound directly to an element.
To remove a live event you should use the die method.