This will work when the parent element is displayed: block:
$('ul li:visible:last').addClass('select');
When the parent element is displayed: none, you must make the element absolute, visible: hidden or place them from the viewport.
if( $('ul').css('display','none') ){ $('ul').css({ 'display':'block', 'position': 'absolute', 'visibility': 'hidden'}); $('ul li:visible:last').addClass('select'); }
source share