Choose only the third lithium

how to select only the third (or some other number of my chioce) li-element with jquery? for example: how to change background of only third li using jquery. any help please

+6
jquery select html-lists background
source share
2 answers

how to choose only the third (

Use the eq method as follows:

 $('li').eq(2).css('background', 'yellow'); 

Or you can use this filter selector option :eq :

 $('li:eq(2)').css('background', 'yellow'); 

Indexing starts at 0 , you need to specify 2 to actually select the third li

If you want to select every third element, you need to use nth-child as follows:

 $('li:nth-child(3n)') 

The nth-child index starts at 1 .

+14
source share

If you need a third li in all lists, use nth-child :

 $('li:nth-child(3)') 
+7
source share

All Articles