English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery :last selector

jQuery Selectors

The :last selector selects the last matched element.

This is usually used with another selector to select the last element in a group (as shown in the following examples).

Note:: The :last selector can only select one element.

use:last-childSelector to select the last child element of its parent element.

Syntax:

$(":last")

Instance

Select the last paragraph:

$(document).ready(function(){
  $("p:last").css("background", "coral");
});
Test See‹/›

Select the last list item:

$(document).ready(function(){
  $("li:last").css("background", "coral");
});
Test See‹/›

Select the last table row:

$(document).ready(function(){
  $("tr:last").css("background", "coral");
});
Test See‹/›

Display: last and :last-Difference between child selectors:

$(document).ready(function(){
  $("#btn1").click(function(){
    $("p:last").css("background-color", "coral");
  });
  $("#btn2").click(function(){
    $("p:last-child").css("background-color", "lightgreen");
  });
});
Test See‹/›

jQuery Selectors