Question :
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!");
}
})
})
Answer :
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 ?