Solved

Use jQuery to add class to last link in a list

Posted on 2009-07-08
5
433 Views
Last Modified: 2012-05-07
Hi all,

Say I have a linked list as such:

<div id="some-list">
<ul>
<li>aaaaa</li>
<li>bbbbb</li>
<li>cccccc</li>
</ul>
</div>

How can I use jQuery to add a class to the last item in the list so it becomes:

<div id="some-list">
<ul>
<li>aaaaa</li>
<li>bbbbb</li>
<li class="some-class">cccccc</li>
</ul>
</div>

Thank you.

GW.
0
Comment
Question by:GhostWerx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 24801969
Use the :last filter
$("#some-list li:last")

Open in new window

0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 50 total points
ID: 24801977
This should do it:
$('#some-list li:last').addClass('some-class');

Open in new window

0
 

Author Comment

by:GhostWerx
ID: 24802060
Hi Guys,

Thanks for the help so far.

Now to make this a little more complicated!!

I have some lists embedded within each other as such

<div id="some-list">
<ul>
<li>aaaaa
   <ul>
   <li>11111</li>
   <li>22222</li>
   </ul>
</li>
<li>bbbbb</li>
<li>cccccc
   <ul>
   <li>33333</li>
   <li>44444</li>
   </ul>
</li>
</ul>
</div>

And I used the following jQuery to add a class to the last link of an embedded list:

$('#some-list ul li ul li:last').addClass('some-class');

The jQuery only added the some-class the the last link of the last embedded list.

How can I get it to add the some-class to the last link of every embedded list???

Thanks for your help so far.

GW.
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 450 total points
ID: 24802114
Loop through each unnumbered list find the last list item and add the class
$("#some-list ul").each(function() {
	$(this).children("li:last").addClass("some-class");
});

Open in new window

0
 

Author Closing Comment

by:GhostWerx
ID: 31601015
Thanks guys.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question