Selector: –> :last used to select the last element.


Now suppose you want to select last p element on the page, then jQuery provides a very useful and powerful selector ":last" that selects the last element. Its usage can be described as follows:

$("p:last")

The above syntax selects the last p element of our page. Now let us look at the short example:

<!--program starts-->

<html>

<head>
<script type='text/javascript'>
$("p:last").css("font-size","20px"); //makes the last p tag contents' size 20px
$("p:last").css("color","#f11"); //makes the text color "red"
</script>
</head>

<body>
<p>hello</p>
<p>See the last p tag is affected</p>
<p>Look i am in red color and have 20px size</p>
</body>
</html>

<!--program ends-->

So the above program changes the color of content or text of the last p tag into "red" with size "20px".
It only affects the last p tag of our page and have no effect on other p tags of our page.
See the difference between ":first" and ":last". The ":first" selector selects first tag and ":last" selector selects last tag.

Leave a Reply

Your email address will not be published.