-5

I'm working on a project, in which when I keep pressing on the min/plus button without hovering off the picture with the .live() method, the function works. In the case of .on() method the function does not work.

How can I fix this issue, so it works for .on() method as well?

Here is an example of what I’m referring too (I fixed the error in this example, but I was using the .on method wrong).

trejder
  • 17,148
  • 27
  • 124
  • 216
nouky
  • 65
  • 1
  • 7
  • 1
    **So to sum up, as suggested by the jQuery experts you should start using .on() and .off() instead of .live() for your next project developments.**. Where are they disagreeing? – Blender Feb 09 '12 at 17:01
  • @Blender: I think *he's* disagreeing with the article. Edited the question for clarity. – josh3736 Feb 09 '12 at 17:03
  • Disagree with an article, fine... but what's the rationale for disagreeing with [the jQuery docs](http://api.jquery.com/on/)? – Sparky Feb 09 '12 at 17:06
  • As you guys can SEE in the DEMO the .on method does not give the same results as the ,live method, please look at the DEMO to understand my issue – nouky Feb 09 '12 at 17:11
  • In Safari, [looking at your demo](http://jsdo.it/nouky/8UQ0), there is absolutely no observable difference between the two cases. – Sparky Feb 09 '12 at 17:29
  • 2
    @nouky Next time before asking a question like this, assume that the jQuery developers know it better than you do. Try reading the manual on how to use a feature correctly instead of stating that the feature does not work. – kapa Feb 10 '12 at 11:30
  • possible duplicate of [jQuery 1.7 - Turning live() into on()](http://stackoverflow.com/questions/8021436/jquery-1-7-turning-live-into-on) – Felix Kling Jan 21 '13 at 15:46

2 Answers2

32

You're not using it correctly. The replacement for .live() is $(document).on() with the event and handler being passed in, of course... for example:

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

It's worth mentioning that before .on() ever came around, .live() was already considered an inefficient way to handle this kind of binding. .delegate() was recommended instead, and now .on() (using the delegator syntax).

Or as an example: instead of the document being the listener (which is what .live() used to do), you should pick the nearest ancestor that does not get destroyed with DOM manipulations. I honestly find the "jsdo.it" a bit clunky to use so I don't have the specific element in mind, but for example, given the structure:

<div id="ajax_container">
 <button id="do_something">Clicky!</button>
 <p>Some dynamically-loaded content</p>
</div>

Where the contents of ajax_container are replaced by an Ajax call (no need to show the code for that part), binding a non-destroyed listener (the container div) for that button's click event would look like:

$('#ajax_container').on('click', '#do_something', function() {
 // do something
})
Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
  • I would use a parent element closer in the DOM to the target element than `document` for performance, but your answer is correct +1. – Rory McCrossan Feb 09 '12 at 17:05
  • Couldn't agree more. Was already writing that update when you wrote your comment. ;-) – Greg Pettit Feb 09 '12 at 17:12
  • 1
    You're welcome! Not everybody loves the .on() syntax, but I dig it... there's a tie back to the old days when we used to use inline onClick (and similar) event binding. ;-) Somehow the syntax feels more intuitive to me than .delegate() ever did. The fact that you need to specify the listener instead of having it implied (like .live()) also makes it more readable. – Greg Pettit Feb 09 '12 at 20:59
0

You'll have to post some code to be certain as to why your implementation was not working, but you should view the jQuery source for examples of how to use .on() and .live()

<div id="parent">
 <a href="#" id="anchor">Click Me</a>
</div>

$('#anchor').live('click',function() { }):
$('#parent').on('click','#anchor',function() { });
$('#anchor').on('click',function() { });

The .live() event listener is added to the document element so that the callback will be fired as long as the event bubbles all the way up to the document element

In the second example ( $('#parent').on() ), the event listener is added to the parent element and is fired every time a click event bubbles up to #parent and comes from (or interacts with along the way) an element named #anchor

The third example ( $('#anchor').on() ) adds the event listener directly to the anchor element itself and is the exact same as $('#anchor').bind('click',function() { });

The reason why .live() was introduced was so that if your page structure changes, event callbacks can still be fired because the event listener itself is attached to the document element.

You can use on in a similar method, but it has to be attached itself to an element that is not removed from the page - if that element is removed, then so are all the event listeners along with it.

http://api.jquery.com/on/ http://api.jquery.com/live/

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • 1
    I think your explanation in the last two paragraphs is a bit off. Reading the jQuery docs [here](http://api.jquery.com/on/) and [here](http://api.jquery.com/live/), _"`.on()` method provides **all** functionality required for attaching event handlers."_. and _"As of jQuery 1.7, the `.live()` method is **deprecated**. Use `.on()` to attach event handlers. "_ – Sparky Feb 09 '12 at 17:32