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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With