If you want to select the last child and need to be specific by type of element, you can use the last-of-type selector
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 above example, both in paragraph 4 and in paragraph 5 there will be a red border, since paragraph 5 is the last element of type "p" in the div, and paragraph 4 is the last "span" in the div.
ScottyG Mar 01 '17 at 16:47 2017-03-01 16:47
source share