jQuery :last-of-type Selector

jQuery | :last-of-type Selector: Learn about the jQuery :last-of-type Selector with its usages, syntax, and examples.
Submitted by Pratishtha Saxena, on November 05, 2022

:last-of-type Selector

In jQuery, selectors are a parameter through which we can select the HTML element. Selectors can comprise – class names, ids, tag names, attributes, etc. Basically, by using selectors, the appropriate HTML-DOM element can be selected and hence performed actions on it. Here, we are discussing the jQuery :last-of-type Selector.

The jQuery :last-of-type selector helps to select all the last child occurrences of the specified tag in their parent elements. It is used along with some tag names prepended to it. If 'div' is specified then, it will return those last occurring divs to their parent element. It can return more than one element.

Unlike :last-child selector, :last-of-type selector returns the last occurrence of the specified tag within the parent element even if that particular type of tag is not the very last child element.

:last-of-type Selector Syntax

$('elementName:last-of-type');

Once the elements are selected, you can access them and perform the actions that you want to perform on them. The example given below shows the selection of the last child element the type 'p' tag on click by passing the appropriate selector.

jQuery :last-of-type Selector Example

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
      <title>Document</title>
      <style>
         div{
         color: darkblue;
         font-size: large;
         font-weight: bold;
         }
         p{
         color: darkgreen;
         font-size: medium;
         font-weight: bold;
         }
         span{
         color: darkgreen;
         font-size: medium;
         font-weight: bold;
         }
      </style>
   </head>
   
   <body>
      <h2>jQuery - Last of type Selector</h2>
      <div>Click the button and get the last of type of the selected element.</div>
      <br><br>
      <button>Last-of-type Selector</button>
      <hr>
      <hr>
      <div>
         Parent Element
         <p> - Child One</p>
         <p> - Child Two</p>
      </div>
      <hr>
      <div>
         Parent Element
         <p> - Child One</p>
         <p> - Child Two</p>
         <span> - Child Three</span>
      </div>
      <hr>
      <div>
         Parent Element
         <p> - Child One</p>
         <p> - Child Two</p>
      </div>
      <hr>
      <hr>
   </body>
   
   <script type="text/javascript">
      $(document).ready(function(){
          $('button').click(function(){
              $('p:last-of-type').css({'color':'red', 'font-size':'larger'});
          });
      });
   </script>
</html>

Output:

Example 1: jQuery :last-of-type Selector
ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.