?
Solved

Use jQuery to add class to last link in a list

Posted on 2009-07-08
5
Medium Priority
?
435 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 200 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 1800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
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.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

741 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