jQuery 3.4.0 is deprecating :first, :last, :eq, :even, :odd, :lt, :gt,
and :nth. When we remove Sizzle, we’ll replace it with a small wrapper
around querySelectorAll, and it would be almost impossible to
reimplement these selectors without a larger selector engine.
We think this trade-off is worth it. Keep in mind we will still support the positional methods, such as .first, .last, and .eq. Anything you can do with positional selectors, you can do with positional methods instead. They perform better anyway.
If you want to select the last child and need to be specific on the element type you can use the selector last-of-type
Here is an example:
$("div p:last-of-type").css("border", "3px solid red");
$("div span:last-of-type").css("border", "3px solid red");
<div id="example">
<p>This is paragraph 1</p>
<p>This is paragraph 2</p>
<span>This is paragraph 3</span>
<span>This is paragraph 4</span>
<p>This is paragraph 5</p>
</div>
In the example above both Paragraph 4 and Paragraph 5 will have a red border since Paragraph 5 is the last element of "p" type in the div and Paragraph 4 is the last "span" in the div.
You can also do this:
:last
.children().last()
:last-child
By using the :last-child selector?
Do you have a specific scenario in mind you need assistance with?
For perfomance:
or if you want a last children with a certain class as commented above.
or a specific child element with a specific class
Hi all Please try this property
Thanks
For 2019 ...
https://blog.jquery.com/2019/04/10/jquery-3-4-0-released/
So you should be now be using
.first()
,.last()
instead (or no jQuery).If you want to select the last child and need to be specific on the element type you can use the selector last-of-type
Here is an example:
In the example above both Paragraph 4 and Paragraph 5 will have a red border since Paragraph 5 is the last element of "p" type in the div and Paragraph 4 is the last "span" in the div.