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

jQuery :nth-last-child() selector

jQuery Selectors

:nth-last-child(n)selector to select all elements that are the nth child of the parent element, starting from the last child element.

Using:nth-last-of-type()Selector to select the specific type of child element from the last child element of its parent element:The nth childAll elements of the element.

Syntax:

$":nth-last-child(number|An+B|odd|even)

Example

Counting from the end, select each as the parent item of the:5of the <p> elements:

$("document").ready(function() {
  -last-child(5")
});
Test and see‹/›

Odd and even keywords are used to match child elements with odd or even indices:

$("document").ready(function() {
  -last-child(even)
  $("li:nth-last-child(odd)
});
Test and see‹/›

Using the formula (An + B) explains:Arepresents a loop size,nis a counter (starting from 0), andBis an offset value:

$("document").ready(function() {
  -last-child(4n + 1")
  $("li:nth-last-child(3n).css("background", "coral");
});
Test and see‹/›

Parameter Value

ParameterDescription
numberThe index of each child element to match (from1End)
oddSelect every odd child element
evenSelect every even child element
An+BSpecify the child element to be selected
Example: p:nth-last-child(3n + 2Select every third paragraph, starting from the second last child

jQuery Selectors