Solved

highlight active menu item

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS Scroll Center with Static Side. Like EE does. 2 30
Text color and background in a text box. 2 26
boostsrap 6 16
Boolean 13 27
This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

730 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