senior
senior

Reputation: 2266

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

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

Answers (4)

Kiran
Kiran

Reputation: 20313

Try this:

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

DEMO

Upvotes: 5

Eduardo Quintana
Eduardo Quintana

Reputation: 2388

Use:

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

Little example

Upvotes: 1

Tom Spee
Tom Spee

Reputation: 9269

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

$("#productsTable:not(:last-child)").empty();

Upvotes: 7

Arun P Johny
Arun P Johny

Reputation: 388316

You can try to use .slice()

$('#productsTable tr').slice(0, -1).remove();

Demo: Fiddle

Upvotes: 0

Related Questions