Solved

Navigation Menu

Posted on 2010-08-21
3
509 Views
Last Modified: 2012-05-10
I need to make a horizontal navigation-menu where an active-class is applied to the buttons with hrefs that match the current url, so I can style that anchor in a way that makes it stand out from the rest of the menu.

Here is the basic menu:

<div class="menu">
        <ul>
          <li><a href="index.aspx"><span>Home Page </span></a></li>
          <li><a href="services.aspx"><span>Services</span></a></li>
          <li><a href="portfolio.aspx"><span>Portfolio</span></a></li>
          <li><a href="about.aspx"><span> About Us </span></a></li>
          <li><a href="contact.aspx"><span> Contact Us</span></a></li>
        </ul>
      </div>

In other words, when a page loads, I want that page's corresponding menu item (button) to show an active class so visitors will know what the current page is.

Something in JQuery would be nice. Regular JS is also OK. I want to do this on the client.

Thank you

0
Comment
Question by:jaysch
[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
3 Comments
 
LVL 9

Expert Comment

by:Snarfles
ID: 33491714
Your list takes the user to a new asp page. Why don't you just do a check for the current page on there rather than attempting to use javascript on a now non existant page?
0
 
LVL 7

Accepted Solution

by:
mcuk_storm earned 400 total points
ID: 33491768
If you do the following, it will find the filename that you are currently on from window.location.pathname then it will find an anchor tag with that href set, find its parent (should be the li) and set a class of active on it. You can then style the active class through css or change the addClass('active') to something else to set the styles like .css({color: '#fff'});

Where it uses jQuery you can replace this with $ if you like.

jQuery('div.menu li a[href=' + window.location.pathname.match(/[^\/]+$/) + ']').parent().addClass('active');

Hope this helps,
0
 

Author Closing Comment

by:jaysch
ID: 34140848
Took a little more tweaking but this solution pointed me in the right direction.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)
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…

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