Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Use jQuery to add class to last link in a list

Posted on 2009-07-08
5
Medium Priority
?
438 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 43

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
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…

618 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