Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing last 3 rows of a table

i have a table and i have two buttons to add and remove rows to this table .

so adding row is easy (i get 3 rows via ajax every time) .

now i want to be able to remove last 3 rows of table by clicking on remove button so unless there is some kind of function or .... to remove recent additions to my table ! i need to remove last three rows of table myself

something like

$('#tbl tr:lt(3)').remove();

but last 3 not first ones !

or i have to do something stupid like

function remove(){
var i ;
for(i=0 ; i < 3 ; i++ ){
$('#table tr:last').remove();
}
alert('removed');
}
like image 261
max Avatar asked Dec 17 '22 08:12

max


2 Answers

$("table tr").slice(-3).remove();
like image 78
Chris Fulstow Avatar answered Dec 26 '22 16:12

Chris Fulstow


It's not so stupid, it's a nice workaround. To clean it up, I'd use

function remove(){
    $('#table tr:last').remove();
    $('#table tr:last').remove();
    $('#table tr:last').remove();
    alert('removed');
}
like image 28
genesis Avatar answered Dec 26 '22 17:12

genesis