Execute jQuery function in dynamically generated table after an AJAX

3

I need to execute the function below, after the AJAX request it has to remove the table row, if I put the code that removes the direct line in the function it removes the line normally, but if I put inside the AJAX success it does not remove the line and runs alert normally at the end.

$('#tabelaMaterial').on('click', '.icon-remove', function (event) {
    $.ajax({
        url: "/RCM/RemoveItemCarrinho",
        type: "POST",
        success: function () {
            $(this).closest('tr').remove();
            alert("Material removido!");
        }
    })
})
    
asked by anonymous 22.10.2014 / 15:55

1 answer

6

It's a matter of scope, this there in success is something else:

$('#tabelaMaterial').on('click', '.icon-remove', function (event) {
    var $this = $(this);
    $.ajax({
        url: "/RCM/RemoveItemCarrinho",
        type: "POST",
        success: function () {
            $this.closest('tr').remove();
        }
    });
});

Related : What is the difference between $ (this) and $ this and this ?

    
22.10.2014 / 16:04