How to select penultimate (last but one) li element from the ul list in jQuery? How to select penultimate (last but one) li element from the ul list in jQuery? jquery jquery

How to select penultimate (last but one) li element from the ul list in jQuery?


If there's only one list, just pass a negative index to the eq()[docs] method.

$("ul li").eq(-2).addClass("extra");

Sounds like you're using an older version of jQuery.

Here's an example that shows it working in jQuery 1.3.2.


$("ul li").last().prev().addClass("foo");