-4

I want to convert .live to .on method as .live is not used much but I am not able to convert it. Please help me to change it.

$(".delete").live('click', function () {
    var id = $(this).attr('id');
    var b = $(this).parent().parent();
    var dataString = 'id=' + id;
    if (confirm("Sure you want to delete this update?")) {
        $.ajax({
            type: "POST",
            url: "delete.php",
            data: dataString,
            cache: false,
            success: function (e) {
                b.hide();
                e.stopImmediatePropagation();
            }
        });
        return false;
    }
});
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
chithon
  • 195
  • 1
  • 5
  • 15

3 Answers3

1
$(".delete").live('click',function()

can be converted to

$(document).on('click', ".delete", function()

Documentation

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

Change:

$(".delete").live('click',function()

To:

$("body").on('click', '.delete', function () {

Solution:

$("body").on('click', '.delete', function () {
    var id = $(this).attr('id');
    var b = $(this).parent().parent();
    var dataString = 'id=' + id;
    if (confirm("Sure you want to delete this update?")) {
        $.ajax({
            type: "POST",
            url: "delete.php",
            data: dataString,
            cache: false,
            success: function (e) {
                b.hide();
                e.stopImmediatePropagation();
            }
        });
        return false;
    }
});

Please also see jQuery 1.9 .live() is not a function and jQuery - how to use the “on()” method instead of “live()”?

Community
  • 1
  • 1
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
0
$("#someArbitraryContextNode").on('click',".delete",function(){});
TGH
  • 38,769
  • 12
  • 102
  • 135