Muntasim
Muntasim

Reputation: 6786

JQuery replacement of live :: `delegate` or `on`

According to the jQuery API Description: live is completely removed in latest version. But its been used in our projects extensively. For example:

$('div.collapsed').live('mouseover', function () {
        TBD.GENERAL.showLoginOther(this);

});

$(".info_bar .filter a, .pagination a").live("click", function () {
    TBD.DHTML.shadeWithLoading($(this).data('container-id'));
    $.getScript(this.href);
    return false;
});

$("form[loading-effect]").live('ajax:before', function () {
    $(this).find('.button_panels, .loading_panels').toggle();
});

.........

etc.

Now if I want to use the latest jquery what will be the correct replacement of live? delegate or on ?

Anticipating a bit explanation. Thanks in advance

Upvotes: 1

Views: 70

Answers (1)

GautamD31
GautamD31

Reputation: 28763

Since .live() is deprecated you better to use .on() like

$('div.collapsed').on('mouseover', function () {

or can use like

$(document).on('mouseover','div.collapsed', function () {

Because

  1. You can’t use .live() for reusable widgets.
  2. stopPropagation() doesn't work with live.
  3. live() is slower.
  4. live() is not chainable.

and the .on() method provides all functionality required for attaching event handlers.

Upvotes: 1

Related Questions