?
Solved

jQuery to add class to link list item based on href value

Posted on 2009-07-08
4
Medium Priority
?
956 Views
Last Modified: 2012-05-07
Hi all,

if I have the following code:

<ul>
<li><a href="/some-page">Some Page</a></li>
<li><a href="/another-page">Another Page</a></li>
</ul>

How can I use jQuery to add the class "selected" to the li based on the href="/another-page".

So jQuery scans the list, if the href value equals "/another-page" then add the selected class to the li element so I would then have the following code:

<ul>
<li><a href="/some-page">Some Page</a></li>
<li class="selected"><a href="/another-page">Another Page</a></li>
</ul>

Thanks for your help.

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
4 Comments
 
LVL 12

Expert Comment

by:Chad Haney
ID: 24810709
This should work.
$(document).ready(function(){
	$('a').each(
		function(){
			if($(this).attr('href')=="/another-page"){
			$(this).addClass("selected");
			}
	});
});

Open in new window

0
 
LVL 12

Accepted Solution

by:
Chad Haney earned 2000 total points
ID: 24810756
Oh sorry just realized you want the class on the li not the anchor tag.
$(document).ready(function(){
	$('ul a').each(
		function(){
			if($(this).attr('href')=="/another-page"){
			$(this).parent().addClass("selected");
			}
	});
});

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 24810791

$("li a[href='/another-page']").parent().addClass("selected")

Open in new window

0
 

Author Closing Comment

by:GhostWerx
ID: 31601424
Thanks heaps.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

800 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