Get last row on jQuery string

We have a simple ul

 <ul> <li>some text</li> <li>some some text</li> <li>text more</li> <li>text here</li> </ul> ul { text-align: center; width: 100px; } li { width: auto; display: inline; } 

So ul can have multiple lines. How to make last li each line inside ul ?

+4
source share
7 answers

If by the last li on each line of ul you mean the last li in each ul , then:

 $('ul li:last-child'); 

However, if you mean that you have li inside the same ul , written on several lines in the source code, and now you want to get the last on each line, then the answer will be that you cannot. The DOM does not care about your newline characters in your code.


Note: the correct way to do this is to give li separate class.

 <ul> <li></li><li></li><li class="last"></li> <li></li><li></li><li class="last"></li> <li></li><li></li><li class="last"></li> </ul> 

Now you can use CSS

 li.last { color: #444 } 

and jQuery

 $('li.last'); 

the right way...

+9
source

see jquery.last ()

 $('ul li').last().css('background-color', 'red'); 
+1
source

Use the selector :last - http://api.jquery.com/last-selector/

 $("ul li:last"); 

and if you are trying to find the last li for several ul , try this:

 var $lasts = $("ul").map(function(){ return $(this).find("li:last"); }); 

working example:

http://jsfiddle.net/hunter/SBsqS/

+1
source

This returns the group "last li on every ul line"

 $("ul li:last"); 
+1
source

To get the last item in a list using jQuery, you can simply use the last() method.

See here for more information: http://api.jquery.com/last/

 var item = $('#myList li').last() 
+1
source
 var theList = document.getElementById('id_of_my_ul'); var theLastItem = theList.childNodes[theList.childNodes.length - 1]; 

I don’t know how you would do it in jQuery, but it cannot be that it is not.

0
source

Try using the selector :last : http://api.jquery.com/last-selector

0
source

All Articles