Reputation: 13206
I'm trying to run a parent remove after an post request but it doesn't seem to be working.
Any idea why? Here is my code. Ideally, I'd like it to remove the element that $currentId finds.
$('#chat').on('click', '.delete', function () {
var $currentId = $(this).parent().attr('id');
$.post("assets/delete.php",{ id: $currentId },function(data)
{
$(this).parent().remove();
}
);
});
Upvotes: 0
Views: 906
Reputation: 1151
After 'post' finishes, your 'this' does not refer to the element you think. You have to use $('#chat').parent().remove()
Edit: Jacks answer is even better, as it does not need to find element with ID = chat. Assigning it to a temporary variable (in his example, that) is a faster option.
Upvotes: 0
Reputation: 13051
It doesn't work because the $(this)
inside $.post
refer to $.post
and not to $('#chat')
.
try this:
$('#chat').on('click', '.delete', function () {
var $currentId = $(this).parent().attr('id');
var that = $(this);
$.post("assets/delete.php",{ id: $currentId },function(data)
{
that.parent().remove();
}
);
});
Upvotes: 1
Reputation: 18462
You changed scope. Try this:
$('#chat').on('click', '.delete', function () {
var parent = $(this).parent();
var $currentId = parent.attr('id');
$.post("assets/delete.php",{ id: $currentId },function(data)
{
parent.remove();
}
);
});
Upvotes: 4