Selector to select the third item in the list?

Suppose the HTML markup looks like this:

<ul> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li><!-- I want to select only this --> </ul> 

Can I select only the third list item in an unordered list item? I was looking through jQuery documentation, but I cannot find any selector that could do this.

+4
source share
3 answers

You can use nth-child selector

 $('ul li:nth-child(3)') 
+14
source
 $("ul li:eq(2)") // 3rd item 

http://api.jquery.com/eq-selector/

+4
source

here you go

 $('ul>li').each(function(i){ if(i == 2) { //your code goes here return; } }); 

amuses

0
source

Source: https://habr.com/ru/post/1304903/


All Articles