Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all but the first N elements from a list using JQuery

I have a list of items in the DOM and would like to prune it to only contain the first N items, perhaps using slice and remove.

What is the syntax in JQuery to remove any items after the first N?

like image 760
justinhj Avatar asked Oct 20 '25 12:10

justinhj


1 Answers

It seems like you are looking for something like this:

JSFiddle

$("body").children().filter(":gt(5)").remove();

The above removes all the direct children of body with an index over 5. This is assuming body is the top of the DOM in question. if not, you can change body to be whatever the head of the DOM you want to "prune" is.

like image 57
Dallas Avatar answered Oct 23 '25 01:10

Dallas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!