Home >>jQuery Tutorial >jQuery :nth-last-child() Selector
jQuery :nth-last-child(n) selector in jQuery is used to selects all elements that are the nth child, of their parent, regardless of type, counting from the last child.
Syntax::nth-last-child(n|even|odd|formula)
Parameter | Description |
---|---|
n | It must be a number which has the first element has the index number 1. |
even | Used to selects each even child element |
odd | Used to selects each odd child element |
formula | It is used to specifies which child element(s) to be selected with a formula (an + b). |
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p:nth-last-child(3)").css("background-color", "orange");
});
</script>
</head>
<body>
<h1>This is a Phptpoint</h1>
<p>The first paragraph in body.</p>
<p>The second paragraph in body.</p>
<div style="border:1px solid;">
<span>This is a span element in div</span>
<p>The first paragraph in div (and the 3rd child in div, counting from the last child).</p>
<p>The second paragraph in div.</p>
<p>The last paragraph in div.</p>
</div><br>
<div style="border:1px solid;">
<p>The first paragraph in div (and the 3rd child in this div, counting from the last child).</p>
<p>The second paragraph in div.</p>
<p>The last paragraph in div.</p>
</div>
<p>The last paragraph in body.</p>
</body>
</html>
The first paragraph in body.
The second paragraph in body.
The first paragraph in div (and the 3rd child in div, counting from the last child).
The second paragraph in div.
The last paragraph in div.
The first paragraph in div (and the 3rd child in this div, counting from the last child).
The second paragraph in div.
The last paragraph in div.
The last paragraph in body.