Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top