Reputation: 3558
How to select all rows of a table except the last 3 of them with jQuery selectors? Thanks in advance.
Upvotes: 1
Views: 175
Reputation: 7328
One solution is to use the :lt
selector with a negative value:
$("#tableId tr:lt(-3)")
As noted by @squint, this method is not optimal in terms of performance. For better performance, use @Vega's answer.
From the :lt
documentation:
Because
:lt()
is a jQuery extension and not part of the CSS specification, queries using:lt()
cannot take advantage of the performance boost provided by the native DOMquerySelectorAll()
method. For better performance in modern browsers, use$("your-pure-css-selector").slice(0, index)
instead.
Upvotes: 2
Reputation: 79860
As @Lye Fisk pointed out, You need the start index to get all rows except last 3
$('#yourtable tr').slice(0, -3) //should return all rows except last 3
You could use jQuery slice with a negative number to pop the end elements.
$('#yourtable tr').slice(-3) //should return the last 3
Upvotes: 5