:last Selector


last selectorversion deprecated: 3.4

Description: Selects the last matched element.

  • version added: 1.0jQuery( ":last" )

As of jQuery 3.4, the :last pseudo-class is deprecated. Remove it from your selectors and filter the results later using .last().

Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it.

Additional Notes:

  • Because :last is a jQuery extension and not part of the CSS specification, queries using :last cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :last to select elements, first select the elements using a pure CSS selector, then use .filter(":last").

Example:

Finds the last table row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>last demo</title>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<table>
<tr><td>First Row</td></tr>
<tr><td>Middle Row</td></tr>
<tr><td>Last Row</td></tr>
</table>
<script>
$( "tr" ).last().css({ backgroundColor: "yellow", fontWeight: "bolder" });
</script>
</body>
</html>

Demo: