6

How do I go about combining this old jQuery code into the v1.7 .on()?

v1.3 .live():

    $('#results tbody tr').live({
    mouseenter:
       function () { $(this).find('.popup').show(); },
    mouseleave:
       function () { $(this).find('.popup').hide(); }
    });

v1.7 .on():

$('#results tbody').on('mouseenter', 'tr', function () {
    $(this).find('.popup').show();
});
$('#results tbody').on('mouseleave', 'tr', function () {
    $(this).find('.popup').hide();
});

I want to pass both event handlers to one .on() call, but keep the brilliant event delegation .on() allows me to do.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
pilau
  • 6,635
  • 4
  • 56
  • 69
  • 2
    Whats' wrong with your second example? You could chain both `on()` calls off one selector, but other than that I see no issues. – Rory McCrossan May 17 '12 at 13:46
  • No, there's no issues, I just want to pass both event handlers in one object, like I do in the first example. Thanks for the super quick reply, by the way. – pilau May 17 '12 at 13:47
  • 2
    TJ. answers the actual question, but Rory is right that in a case like this you could just use a single, dyanmic handler method. – JMM May 17 '12 at 13:54

4 Answers4

11

You can pass an event-map as the first parameter:

$('#results tbody').on({
    'mouseenter' : function () {
        $(this).find('.popup').show();
     },
    'mouseleave' : function () {
        $(this).find('.popup').hide();
    }
}, 'tr');

jQuery documentation:

.on( events-map [, selector] [, data] ),
events-map A map in which the string keys represent one or more space-separated event types and optional namespaces, and the values represent a handler function to be called for the event(s).

T. Junghans
  • 11,385
  • 7
  • 52
  • 75
3

I just want to pass both event handlers in one object, like I do in the first example.

In this case you could attach the two events together, then differentiate them in the handler itself, like this:

$('#results tbody').on('mouseenter mouseleave', 'tr', function (e) {
    if (e.type == "mouseenter") {
        $(this).find('.popup').show();
    }
    else {
        $(this).find('.popup').hide();
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • This is nice! I think it would come in handy in cases where you are binding many event handlers on the same element - and you could use a select switch to capture everything. – pilau May 17 '12 at 13:55
1

The formats used are specified in the documentation for live

$(document).on({...events...}, selector, data);

-or-

$(document).on(event, selector, data, callback);

The code for the live function in 1.7+ is now just a pass-through function:

live: function( types, data, fn ) {
    jQuery( this.context ).on( types, this.selector, data, fn );
    return this;
}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
-1

Taking your first example and changing live to on should be all that you need

 $('#results tbody tr').on({
mouseenter:
   function () { $(this).find('.popup').show(); },
mouseleave:
   function () { $(this).find('.popup').hide(); }
})

See: http://api.jquery.com/on/#example-6

Rob Lowe
  • 827
  • 6
  • 10