Solved

Navigation Menu

Posted on 2010-08-21
3
505 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
3 Comments
 
LVL 9

Expert Comment

by:Snarfles
Comment Utility
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
Comment Utility
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
Comment Utility
Took a little more tweaking but this solution pointed me in the right direction.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now