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
Solved

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

Posted on 2009-07-08
4
952 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
  • 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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…

808 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