Follow by Email

Friday, December 5, 2014

Tutorial for Selecting nth last element using jquery || using nth-last-child in jquery

hi let see how to select a nth last element like the <div>, <p>, <h1> etc. using jquery.
Now in below example just to check if we have selected the correct element we will change its color
example for selecting a 2nd last <h1> element which is inside a <div>
<!DOCTYPE html>
<html xmlns=””&gt;
<script src=”//”></script>
<script type=”text/javascript”>
$(document).ready(function () {
$(“button”).click(function () {
$(“h1:nth-last-child(2)”).css(“background-color”, “yellow”);
<h1>im 1st h1</h1>
<h1>im 2nd h1</h1>
<h1>im 3rd h1</h1>
<h1>im 4th h1</h1>
<h1>im 5th h1</h1>
<button>Change color of 4th element ( as it is 2nd last )</button>