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

73 views Asked by At

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

1

There are 1 answers

4
GautamD31 On

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.