Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using jquery, remove all the table elements except the last one

Tags:

html

jquery

In my HTML page, I have a table with the id ="productsTable", I wanna remove all its elements except the last one.

I started by this code which will remove all the elements:

$('#productsTable').empty();
like image 747
senior Avatar asked Oct 15 '25 08:10

senior


2 Answers

Don't know the last HTML element but something like this?

$("#productsTable:not(:last-child)").empty();
like image 135
Tom Spee Avatar answered Oct 17 '25 00:10

Tom Spee


Try this:

$("#productsTable").find("tr:not(:last)").remove();

DEMO

like image 23
Kiran Avatar answered Oct 17 '25 00:10

Kiran