jQuery :last-of-type Selector

The jQuery :last-of-type selector selects all the elements which are the last child of matching type of its parent. The examples given here contains the table element and the list element.

Let’s see the examples to select the elements using the selector.

live demo jquery last of type selector

Syntax of jQuery :last-of-type Selector

The syntax of the jQuery :last-of-type selector is given below.

Syntax
$(“:last-of-type”)

If require no arguments to pass and select the elements. However, you have to use the tag name before the selector name to select the matching element. For example, if you want to select the row of a table, you have to specify the tag name with the selector as $("table tr:last-of-type").

jQuery :last-of-type Selector Select Last Element of List

Let’s select the last child item of the list element as given below. The example contains the two list elements with items. The selector selects the last child of both the list element in the example below. It uses the click event to perform the task to select the items.

Example

Test it Live

Output

  • Mobile
  • Computer
  • Laptop
  • Tablet
  • Android
  • Windows
  • Linux
  • Java

You have to click the button given in the example above. When you click the button, the selector selects the last items. It applies the ‘orange’ background color to the list items. You can also use the other effects of jQuery after selecting the items.

Select the Last Element in a Cell of a Table

Let’s take another example of the table to select an elements using the selector. It selects the span elements of the cells of the table given below. Here in the example, the selector selects all the span elements as they all are the last child of their table cells.

Example

Test it Live

Output

Sr No Name
1 Core Java
2 PHP Functions
3 jQuery Reference
4 Excel Functions
5 PHP Functions

Click the button given above to select the cells of the table. It applies the ‘red’ color when you click the button. If you specify the td element to select with the selector, it selects all the td element and applies the color on button click.

I hope you like this tutorial of jQuery :last-of-type selector to select all the last-child items of their parent. If you have any queries regarding the tutorial, please comment below.

Also tell me, what other methods you are using with the selector by commenting below.

References