The :nth-last-of-type(n) Selector is used to select the elements, who are the 'n'th child of their parent of the same type from the end.
Let us simplify with the below example.
<html>
<head>
<title> My First Programme </title>
</head>
<body>
<h1> JQuery </h1>
<div class = "newClass">
<p class = "para1"> First Paragraph </p>
<p class = "para2"> Second Paragraph </p>
<p class = "para3"> Third Paragraph </p>
<span> My Span </span>
</div>
<button> Click me </button>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script>
<script>
$('button').click( function() {
$('p:nth-last-of-type(1)').text("The last child of div element of p type got replaced")
});
</script>
</body>
</html>
So, if you see the above code. We can see that there are three <p> elements and a <span> element, those are inside a <div> element.
<div class = "newClass"> <p class = "para1"> First Paragraph </p> <p class = "para2"> Second Paragraph </p> <p class = "para3"> Third Paragraph </p> <span> My Span </span> </div>
Now, if you consider the DOM,
-Selector1.png)
The <div> element has three children of type <p>.
<p class = "para1"> First Paragraph </p> <p class = "para2"> Second Paragraph </p> <p class = "para3"> Third Paragraph </p>
And one child of <span> type,
<span> My Span </span>
And on button click, the contents of third <p> element gets replaced with The third child of div element of p type got replaced.
And this happened with the :nth-last-of-type(1) element selector.
$('button').click( function() {
$('p:nth-last-of-type(1)').text("The last child of div element of p type got replaced")
});The moment the button is clicked, JQuery statement gets triggered.
$('p:nth-last-of-type(1)').text("The last child of div element of p type got replaced")And the JQuery code locates the second child of <div> element of <p> type and changes its contents.
-Selector2.png)