jquery addClass in second last <li> tag

I want to add a class to the second last <li> element in every <ul> tag.

I've tested this in a html page which works.

      $('ul.sub_menu li').last().prev().addClass('secondLast');      

but it doesnt' work when i place this into the default.aspx file

website: http://asqastage.esa.edu.au/

this ul.submenu is in the top menu nav (i.e. Home, About Us, The national system, etc.)
jeremyllAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
what about this
 $('ul.sub_menu li:last').prev().addClass('secondLast');      
or
 var selector = $('ul.sub_menu li");
 selector.eq( -1 ).prev().addClass('secondLast');      
jeremyllAuthor Commented:
thanks gurvinder372

I've tried both of them and neither worked.
David S.Commented:
This should do it:
$('ul').each(function(){
  $(this).children(':last-child').prev().addClass('secondLast');
});

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Gurvinder Pal SinghCommented:
but where have you added this code at first place?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jeremyllAuthor Commented:
thanks Kravimir. I've tried that, which didn't work.

To be honest I rekcon Kravmir's and gurvinder372's and even my code (i.e. $('ul.sub_menu li').last().prev().addClass('secondLast');      

should be working because I've tested it in a plain html page which works!

Something amiss.
jeremyllAuthor Commented:
Good question gurvinder372. My bad sorry guys! I placed it in the <head> tag in default.aspx instead of after the </body> tag.

That should be the case right?

I've tried it and it worked.
Gurvinder Pal SinghCommented:
Yes, that should be the case.
Since when i opened your code in my debugger (safari 4), i couldn't see where that piece of code is
David S.Commented:
Putting it after the </body> is not valid. It could be placed immediately after it instead.

If you wanted to put it in the head, you could use the "ready()" event handler:
$(document).ready(function(){
  $('ul').each(function(){
    $(this).children(':last-child').prev().addClass('secondLast');
  });
});

Open in new window

khan_webguruCommented:
hello Bro!


Whats the status of the problem

The code is fine and should be work

 
$('ul').each(function(){

 		 $(this).children(':last-child').prev().addClass('secondLast');

	 });

Open in new window


make sure that would be in <Head> tag and also if you want to add on proper location then add this in ready function like this

 
$(document).ready(function(){
  $('ul').each(function(){

 		 $(this).children(':last-child').prev().addClass('secondLast');

	 });

});

Open in new window



Regards,

Asif Ahmed khan
jeremyllAuthor Commented:
There's notthing wrong. I tried to award points to Kravimir and gurvinder372 12 hours ago, but it's created a close request instead.

experts exchange webstie was down for a while earlier today.
jeremyllAuthor Commented:
legendary
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.