How to select last child element in jQuery?

lovespring picture lovespring · Jan 6, 2011 · Viewed 140.1k times · Source

How to select last child element in jQuery?

Just the last child, not its descendants.

Answer

Yoram de Langen picture Yoram de Langen · Jan 6, 2011

You can also do this:

<ul id="example">
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    <li>Fourth</li>
</ul>

// possibility 1
$('#example li:last').val();
// possibility 2
$('#example').children().last()
// possibility 3
$('#example li:last-child').val();

:last

.children().last()

:last-child