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?
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');      
0
jeremyllAuthor Commented:
thanks gurvinder372

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

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

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.
0
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.
0
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
0
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

0
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
0
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.
0
jeremyllAuthor Commented:
legendary
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.