Link to home
Start Free TrialLog in
Avatar of jeremyll
jeremyll

asked on

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.)
SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jeremyll
jeremyll

ASKER

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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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