Select last 5 elements with jQuery

Viewed 30296

Is it possible to select the last 5 child divs of an element?

3 Answers

Yes, you can get the div elements, and then use the slice method to get the last five:

var e = $('#someelement > div').slice(-5);

Sure, you could use the .length property of a selector to get the count, and then use the :gt(n) selector to get the last 5.

var o = $("div.container > div:gt("+($("div.container > div").length-5)+")");
Related