Example 1 — Official Demo: ul li:nth-last-child(2)
Mark the second-to-last list item in every ul (jQuery API example).
$("ul li:nth-last-child(2)").each(function () {
this.textContent = this.textContent + " - 2nd to last!";
});
console.log($("ul li:nth-last-child(2)").length); How It Works
- Scan each list. Every
ulhas its own child positions from the end. - Require index 2 from the end. The penultimate child is the candidate.
- Require an
li. That child must also be a list item.

