Reputation: 2266
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();
Upvotes: 4
Views: 3117
Reputation: 9269
Don't know the last HTML element but something like this?
$("#productsTable:not(:last-child)").empty();
Upvotes: 7
Reputation: 388316
You can try to use .slice()
$('#productsTable tr').slice(0, -1).remove();
Demo: Fiddle
Upvotes: 0