Solved

highlight active menu item

Posted on 2013-01-06
4
235 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
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 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 invoke it on a web page. 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.: (CODE)

822 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