jQuery Tutorial Tutorials - jQuery :eq() selector

:eq() selector

The :eq() selector get the n-th element within the matched set.

jQuery :eq() selector syntax

$(':eq(index)')

index is the index of the element based on 0.
If index is a negative number, it count backwards from the last element.

jQuery :eq() selector Examples

Below example illustrates the jQuery :eq selector.

selector selected element
$('li:eq(0)') item1
$('li:eq(1)') item2
$('li:eq(-1)') item4
$('li:eq(-2)') item3
<ul id="navList">
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
</ul>
<script>
    console.log($('li:eq(0)').html());  // item1
    console.log($('li:eq(1)').html());  // item2
    console.log($('li:eq(-1)').html()); // item4
    console.log($('li:eq(-2)').html()); // item3
</script>

Try now

Date:2019-08-18 01:06:45 From:www.Lautturi.com author:Lautturi