views:

687

answers:

3

What is the exact difference between live() and ready()?


Edit: found that die() is the opposite of live()

+6  A: 

.ready() lets you register a callback that fires when the DOM is ready - this is similar to using window.onload but fires earlier (and you can register more than one callback).

.live() lets you register a callback to a range of events based on a selector, which continually monitors the DOM and will register itself to new nodes that are added.

Greg
Thanks, so ready() isn't fired again when the DOM is maniplulated again?
powtac
@powtac, no it's not. Only when the DOM initially loads.
Sixten Otto
@powtac: correct
Greg
+1  A: 

live is used for attaching events to a current selector and all future matching selectors.

ready binds a function to be executed whenever the DOM is ready to be traversed and manipulated

brad
Why not use live() in favor of ready() ?
powtac
Sorry, I mean: why not use live() instead of ready() ?
powtac
that live isn't supported for all events is probably the main reason
FinnNk
Which elements are not suported by live() ? document?
powtac
i think you're not thinking of this properly. YOu want to use ready to know when the DOM is ready for manipulation. Your live() usage will be inside the ready call. They're not really comparablehttp://docs.jquery.com/Events/live lists all supported elements
brad
submit and change aren't supported with live() at the moment, although they're on the roadmap for 1.4
FinnNk
+1  A: 

ready() fires once after the DOM has finished loading and is ready to be traversed and manipulated. Basically a replacement for the old trustworthy onload event on window (similar but not identical)

With live() you specify a selector and jQuery then attaches the function you specify as second argument to all elements matched now and in future (add to to DOM dynamically) for the event specified as first argument

jitter