0

The on() method isn't supported in jQuery 1.4.x. Can someone tell me how to fix this so it's compatible?

$(document).on({
  mouseenter: MyObj.mouseenter,
  mouseleave: MyObj.mouseleave
}, '.class');
Zoolander
  • 2,293
  • 5
  • 27
  • 37

3 Answers3

3

Use the delegate method.

$(document).delegate('.class',{
  mouseenter: MyObj.mouseenter,
  mouseleave: MyObj.mouseleave
});

Note: jQuery 1.4.3+ required

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • Just wondering, is $(document).delegate() equivalent to live? – A. Wolff Apr 12 '13 at 18:05
  • 1
    No, it is far more efficient. – Kevin B Apr 12 '13 at 18:06
  • 1
    @KevinB: It's no more efficient when applied to the `document`. –  Apr 12 '13 at 18:07
  • 1
    Other than the fact that it doesn't try to select all existing .class elements then do nothing with them. Though, in practice, yes they'll be about the same when using document. – Kevin B Apr 12 '13 at 18:08
  • 1
    @KevinB: Come on... that's a one-time DOM selection in the beginning. Yes it's wasted, but is trivial when compared to the overall picture. –  Apr 12 '13 at 18:09
  • @amnotiam to answer my own question – A. Wolff Apr 12 '13 at 18:12
  • As far as the one time dom selection in the beginning, consider how much of a delay $(".row") can have if it was a class on every row of a table that had 100+ rows, that can definitely begin to impact performance in a noticeable way in some browsers. – Kevin B Apr 12 '13 at 18:18
  • @KevinB: Yes, especially in IE7 and lower *(no qSA)*, though if using `.live()`, there's no reason to wait for the DOM to be ready when setting up the handler. It should generally be done in the `head` without waiting. That way there's a very minimal DOM. Either way, it's good that jQuery finally had the sense to deprecate `.live()`. Too many people didn't understand it, and used it for everything. –  Apr 12 '13 at 18:31
2

You should use live instead of on to make it compatible with old versions of jQuery. live always works for the elements that will be created after the page has been loaded.

jQuery 1.7 deprecated live() method and 1.9 has removed it. It has been replaced by on.

If you can however, it is wiser to just upgrade your jQuery.

Do not use bind because this offers not the same functionality as live.

Christian-G
  • 2,371
  • 4
  • 27
  • 47
0

You could apply various listeners directly to a jQuery object like so:

$(".class").mouseover(function() {

}).mouseout(function() {

});

... or this:

$(".class").mouseenter(function() {

}).mouseleave(function() {

});

Example available here: http://api.jquery.com/mouseenter/

diggersworld
  • 12,770
  • 24
  • 84
  • 119
  • 2
    You are not using delegation here – A. Wolff Apr 12 '13 at 18:06
  • The code example in the question is limited. The developer may not have required the use of `.on()` in the first place. Either way we're unable to tell currently. – diggersworld Apr 12 '13 at 18:12
  • @diggersworld he actually used delegation in his example. as you can see he bound the handlers to the document and has `'.class'` as his selector – wirey00 Apr 12 '13 at 18:17
  • @wirey: agreed, however what's to say he didn't just copy the code from somewhere else without knowing what it does. – diggersworld Apr 12 '13 at 18:20