FFish FFish - 2 years ago 226
Javascript Question

jquery selector before-last

I have a dynamic list and need to select the before last item.

<ul class="album">
<li id='li-1'></li>
<!-- ... -->
<li id='li-8'></li>
<li id='li-9'></li>
<li class='drop-placeholder'>drag your favorites here</li>

var lastLiId = $(".album li:last").attr("id"); // minus one?

Answer Source

You can use .eq() with a negative value (-1 is last) to get n from the end, like this:

$(".album li").eq(-2).attr("id"); // gets "li-9"

You can test it here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download