Consider following code snippet:

<div>
    <p>Something_1</p>
    <p>Something_2</p>
</div>
<div>
    <p>Something_3</p>>
</div>
<script>
    $("div p").css('color','red');
    $("div p:last-child").css('color','green');
    $("div p:last").css('color','blue');
</script>
What will be the colors of p elements after execution the code?
Explanation
The :last selector selects only the last element - The :last-child selector selects all elements that are the last child of their parent.

$("div p").css('color','red'); // will make all p elements to be red;
$("div p:last-child").css('color','green'); // will make two last element to be green;
$("div p:last").css('color','blue'); // selects only the last element and make it to be red;

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback