Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Remove a table row after it's deleted from database

Tags:

jquery

I'm try to remove the deleted table row after it's been successfully deleted from the database but it not working. Here is my code.

This works:

$('table td img.delete').click(function(){
    if(confirm("Are you sure you want to permanently delete this?"))    
    $(this).parent().parent().remove();
    }
});

This doesn't work:

$('table td img.delete').click(function(){
    if(confirm("Are you sure you want to permanently delete this?"))    
    $.get('cfc/action.cfc?method=deleteLetter&returnformat=plain', {id: $(this).attr('id')},
        function(data){
            if(data.replace(/^\s+|\s+$/g, '')==='ok'){
            $(this).parent().parent().remove();
            }
        });
    }
});

This record get deleted here but the table row remains. Any suggestion would be much appreciated.

thanks!

like image 594
user281867 Avatar asked Feb 18 '26 21:02

user281867


1 Answers

Use closest to find the nearest tr element which is the row you want to delete. And also you need to maintain the current row in a variable because in the success handler of $.get, this will not point to img element. Try this

$('table td img.delete').click(function(){
    var $this = $(this);
    if(confirm("Are you sure you want to permanently delete this?"))    
    $.get('cfc/action.cfc?method=deleteLetter&returnformat=plain', {id: $(this).attr('id')},
        function(data){
            if(data.replace(/^\s+|\s+$/g, '')==='ok'){
            $this.closest('tr').remove();
            }
        });
    }
});
like image 118
ShankarSangoli Avatar answered Feb 21 '26 13:02

ShankarSangoli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!