Home >>jQuery Tutorial >jQuery :nth-last-of-type() Selector

jQuery :nth-last-of-type() Selector

jQuery :nth-last-of-type() Selector

jQuery :nth-last-of-kind(n) selector in jQuery is used to pick all elements that are their parent's nth child, of a specific type, counting from the last child.

Syntax:
:nth-last-of-type(n|even|odd|formula)

Parameter Values

Parameter Description
n It must be a number which has the first element has the index number 1.
even It is used to selects each even child element
odd It is 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).
Here is an Example of jQuery :nth-last-of-type() Selector:

<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-of-type(3)").css("background-color", "orange");
});
</script>
</head>
<body>
<h1>This is phptpoint</h1>
<p>The first paragraph in body (and the 3rd paragraph in body, counting from the last child).</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 paragraph 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 paragraph, 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>

Output:

This is phptpoint

The first paragraph in body (and the 3rd paragraph in body, counting from the last child).

The second paragraph in body.

This is a span element in div

The first paragraph in div (and the 3rd paragraph 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 paragraph, counting from the last child).

The second paragraph in div.

The last paragraph in div.

The last paragraph in body.


No Sidebar ads