I've seen a lot of questions about how to select all but the first and last rows of a table, but I want to know how to select everything except the first TWO rows and the last. Right now I have this to select all but the first two:
$("#sometable").find("tr:gt(1)").remove();
and I need some way to get the last one as well. Maybe some way to combine with
:not(:last)
?
I'm fairly new to jQuery and any help would be appreciated.
The jQuery function removes all the rows except the first one using the remove() method. Syntax: $('#btn').
The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements.
This is what you need:
$("#sometable").find('tr').slice(2,-1).remove()
DEMO
jsperf of my and @Evan's solutions.
Just combine the selectors:
$("#sometable").find("tr:gt(1):not(:last)").remove();
JSFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With