1

With the newer jQuery .click(), .submit(), etc. was replaced by:

$('#foo').on('click', function() {
    //
});

in terms of best practice. Did anything similar happen to .live()? Or is this still the best way to do it?

$('#foo').live('click', function() {
    //
});
  • See [.on()](http://api.jquery.com/on/) && [.off()](http://api.jquery.com/off/) – SpYk3HH Nov 30 '12 at 21:54
  • Didn't `on` replace `live` as well? – Dave Newton Nov 30 '12 at 21:54
  • 1
    Voting to close, since this is easily answered by reading the [jQuery docs](http://api.jquery.com/live) – Blazemonger Nov 30 '12 at 21:57
  • 1
    @Blazemonger: If you believe this question does not show any research effort than that is what the downvote feature is for, according to it's tool-tip anyway. Lack of research is not a good reason to close question in my opinion or we would be closing nearly all basic questions. I'm sure though that several duplicates can be found of this question and that would be a valid reason for closing a question. The current 2 votes on closing for reasons of the question being to localized is not the correct reason though, nor is lack of research. – Nope Dec 01 '12 at 01:43

4 Answers4

3

Delegation using .on() is the correct way instead of using live

$('staticAncestorElement').on('click','#foo',function(){

});

as you can see from the jQuery live() documents

$(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+

Community
  • 1
  • 1
wirey00
  • 33,517
  • 7
  • 54
  • 65
3

On() has all the abilities of all other bindings. To bind to dynamic elements you can use on() like this:

$(document).on('click', '#foo', function() {
    //
});

Preferably you want to use a close static element instead of document.

To quote from another post of mine regarding the many binding methods jQuery has on offer:

bind() was added in 1.0, live() in 1.3, delegate() in 1.4.2 and on() in 1.7.

As of 1.7 on() is the preferred use and live() is deprecated and not recommended at all. If you are using 1.3 use bind() instead of live() and as of 1.4.2 use delegate() instead of live() and as of 1.7 use on() instead of any of the others.

You can see the full post here, which also list the many drawbacks of live() and why it should not be used any more in jQuery 1.7 or later.

Community
  • 1
  • 1
Nope
  • 22,147
  • 7
  • 47
  • 72
1

With the newer jQuery .click(), .submit(), etc. was replaced by

They weren't replaced. click and submit still work but now they are shortcuts to on. The methods that are deprecated are bind and live. Now you can do this to delegate events:

$(closestStaticParent).on('click', 'element', function(){ ... })
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

.click was not replaced with .on("click". Read the documentation.

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

.live was replaced by .on and .off.

Other functions like .click will continue to work as expected

SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
  • If you got `off`, you might as well list [die](http://api.jquery.com/die/) for completness :) – Nope Nov 30 '12 at 22:04