Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select last 5 elements with jQuery

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

like image 812
Adam Avatar asked Dec 22 '09 18:12

Adam


People also ask

How to select last element in jQuery?

The :last selector selects the last element. Note: This selector can only select one single element. Use the :last-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the last element in a group (like in the example above).

How can select last column of table using jQuery?

Approach 2: Use $('table tr:last') jQuery Selector to find the last element of the table. The 'table' in query looks for the table element then 'tr' is looking for all the rows in the table element and ':last' is looking for the last table row of the table.

How do I get the second last Li in jQuery?

You need to use "nth-last-child(2)" of jquery, this selects the second last element.

Is last child jQuery?

Definition and UsageThe :last-child selector selects all elements that are the last child of their parent. Tip: Use the :first-child selector to select elements that are the first child of their parent.


2 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); 
like image 62
Guffa Avatar answered Sep 29 '22 18:09

Guffa


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)+")"); 
like image 33
Sampson Avatar answered Sep 29 '22 17:09

Sampson