jQuery | :nth-last-of-type() Selector

The :nth-last-of-type() Selector is used to select all child of a parent with same element name, counting from the last to first. It uses 1-indexed counting.

Syntax:

element:nth-last-of-type(n|even|odd|formula)

Parameters:



  • n: It select nth element from last.
  • even: It select every even element from last.
  • odd: It select every odd element from last.
  • formula: It holds the formula to generate grouping of element, starting from last, For example: 3n+2 selects every element which is output of this expression by taking values of n-0, 1, 2, …

Example 1: This example use :nth-last-of-type() selector to select odd element from last.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        jQuery | :nth-last-of-type() Selector
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    Computer Science Subjects
    <ul>
        <li>Data Structure</li>
        <li>Algorithm Analysis</li>
        <li>Operating System</li>
        <li>Computer Networks</li>
        <li>Web Technology</li>
        <li>C Programming</li>
        <li>C++ Programming</li>
        <li>Java</li>
    </ul>
      
    <!-- Script to use :nth-last-of-type() Selector -->
    <script>
        $(document).ready(function() {
            var el = $("ul li:nth-last-of-type(odd)").css({
                background: "green"
            });
        });
    </script>
</body>
  
</html>                    

chevron_right


Output

Example 2: This example use :nth-last-of-type() selector to select last element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        jQuery | :nth-last-of-type() Selector
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <section>
        <article>
            <p>Company - GeeksforGeeks</p>
            <p>Name - ABC</p>
            <p>Email - abc@geeksforgeeks.org</p>
        </article>
        <article>
                <p>Company - GFG</p>
                <p>Name - XYZ</p>
                <p>Email - xyz@gfg.org</p>
        </article>
        <article>
                <p>Company - G4G</p>
                <p>Name - Geeks</p>
                <p>Email - geeks@g4g.org</p>
        </article>
    </section>
      
    <!-- Script to use :nth-last-of-type() selector -->
    <script>
        $(document).ready(function() {
            $("article p:first-of-type").css({
                fontWeight:"bold",
                fontSize:'1.2rem'
            })
            $("article p:nth-last-of-type(1)").css({
                fontStyle:'italic',
                opacity:0.8
            })
        })
    </script>
<body>
      
</html>                    

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.