Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select only third li

how do i select only 3rd( or some other no. of my chioce) li element with jquery? for ex: how do i change the background of only third li with jquery. any help please

like image 619
sonill Avatar asked Aug 09 '10 09:08

sonill


1 Answers

how do i select only 3rd(

Use the eq method like this:

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

Or you can use this variation of :eq filter selector:

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

The indexing starts from 0, you need to specify 2 to actually select the third li

If however you want to select every third element, you need to use nth-child like this:

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

The index for nth-child starts from 1 though.

like image 153
Sarfraz Avatar answered Oct 04 '22 04:10

Sarfraz