jQuery | :first-of-type Selector

The :first-of-type Selector is used to select all elements those are the first child, of a particular type, of their parent.

Syntax:

$(":first-of-type")

Below examples illustrate the :first-of-type selector in jQuery:



Example 1: This example changes the background-color to green and text-color to white, of the first heading of their parents (div tags).

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>  
<html>  
  
<head
    <title
        jQuery | :first-of-type Selector
    </title>
          
    <script src=
    </script>
      
    <!-- Script to use first-of-child selector -->  
    <script>
        $(document).ready(function() {
            $("h4:first-of-type").css({
                "background-color": "green", 
                "color": "white"
            });
        });
    </script>
          
    <style>
        option {
            font-weight: bold;
            font-size: 25px;
            color: green;
        }
        select {
            font-weight: bold;
            font-size: 25px;
            color: green;
        }
    </style>
</head
      
<body style="text-align:center; width: 500px; margin: auto">  
    <h1>  
        jQuery | :first-of-type Selector
    </h1>  
  
    <div style="border:1px solid blue;">
        <h4>The first heading in first div.</h4>
        <h4>The last heading in first div.</h4>
    </div><br>
   
    <div style="border:1px solid blue;">
        <h4>The first heading in second div.</h4>
        <h4>The last heading in second div.</h4>
    </div>
</body>  
  
</html>       

chevron_right


Output:

Example 2: This example changes the background-color to green and text-color to white, of the first heading of <body> tag.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>  
<html>  
  
<head
    <title
        jQuery | :first-of-type Selector
    </title>
  
    <script src=
    </script>
      
    <!-- Script to use first-of-child selector -->
    <script>
        $(document).ready(function() {
            $("h4:first-of-type").css({
                "background-color": "green", 
                "color": "white"
            });
        });
    </script>
      
    <style>
        option {
            font-weight: bold;
            font-size: 25px;
            color: green;
        }
        select {
            font-weight: bold;
            font-size: 25px;
            color: green;
        }
    </style>
</head
  
<body style = "text-align:center; width: 500px; margin: auto">  
    <h1>  
        jQuery | :first-of-type Selector
    </h1>  
      
    <h4>The first heading in body.</h4>
   
    <h4>The last heading in body.</h4>
</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.