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

highlight active menu item

Posted on 2013-01-06
4
237 Views
Last Modified: 2013-01-07
it's a standard menu using a UL & LI and a href's.  I want to show a special color on the menu item for the currently displayed page.

How is this typically done?

thanks
0
Comment
Question by:tf2012
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38748885
$(".menuItem").each(function() { 
     if( location.href.indexOf( $(this).attr("href") ) >= 0 ) $(this).addClass("special_color");
     else $(".menuItem").removeClass("special_color");
});

Open in new window

AND/OR :
$("a.menuItem").click(function(evt) {
    evt.preventDefault();
    $(".menuItem").removeClass("special_color");
    $(this).addClass("special_color");
});

Open in new window

With CSS :
.special_color {
   background:red;
}

Open in new window

0
 
LVL 1

Author Comment

by:tf2012
ID: 38748975
@leakim971

I'd like to try it but my a href's don't have classes, they are generated by wordpress.  The parent li, does however, have a class of 'menu-item'

Can you show me how to reference 'li.menu-item a' with the script you posted?

thanks!
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38749002
$("li.menu-item a").each(function() { 
     if( location.href.indexOf( $(this).attr("href") ) >= 0 ) $(this).closest("li.menu-item").addClass("special_color");
     else $(this).closest("li.menu-item").removeClass("special_color");
});

Open in new window


if it don't work, please provide a link to your site.
0
 
LVL 1

Author Closing Comment

by:tf2012
ID: 38750917
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 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