Solved

Use jQuery to add class to last link in a list

Posted on 2009-07-08
5
423 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html form to write data to csv 24 95
How to drill down using class name and find the text? 25 31
Show banners that are blocked by Ad Muncher 10 44
Centering Vertically 3 27
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now