Solved

Navigation Menu

Posted on 2010-08-21
3
507 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn how to dynamically set the form action using jQuery.
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)

774 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