Select nth Last Child Elements $(":nth-last-child(n)")

$(":nth-last-child(n)")

e.g. $("div:nth-last-child(n)") n:{integer e.g. [1,2,...,n], even, odd or equation e.g. (n+2) n= [0,1,...,n] }

$(":nth-last-child(n)") selects elements if they arenth child of their parents starting from the last.

$(":nth-last-child(1)") is equivalent to $(":last-child")
Example 1: $("li:nth-last-child(1)") will select all <li> elements if they are 1st last child of their parents starting from last.

<ul>
<li>1st</li>
<li>2nd</li>
<li>3rd</li>  //last index=1 
</ul>
    
<ul>
<li>1st</li>
<li>2nd</li>
</ul>
Example 2: $("li:nth-last-child(odd)") will select all <li> elements if they are odd child of their parents starting from last.

<ul>
<li>1nd</li>  // index = 4 
<li>2st</li>  // index = 3 
<li>4nd</li>  // index = 2
<li>4rd</li>  // index = 1 starting from last 
</ul>
    
<ul>
<li>1nd</li>
<li>2st</li>  // index = 1 starting from last 
</ul>
Example 3: $("li:nth-last-child(n+2)") will select all <li> elements if they are (n+2) child starting from last of their parents .
Notice: for equation, n starts from zero

<ul>
<li>1st</li>  // index = 3 (1+2)  
<li>2nd</li>  // index = 2 (0+2)  
<li>3rd</li>  // index = 1 starting from last 
</ul>
    
<ul>
<li>1st</li>  // index = 2 (0+2)  
<li>2nd</li>  // index = 1 starting from last 
</ul>
Example 4: $("span:nth-last-child(3)") will find all <span> elements if they are 3rd child starting from last of their parents.
<div>
  <p>a</p> // 3rd child but NOT <span>
  <span>b</span>
  <span>c</span>
</div>
<div>
  <span>d</span> // 3rd child and <span>
  <p>e</p> 
  <span>f</span>
</div>
jQuery selector filter $(":nth-last-child(n)")