Jump to content

Highlight Current Page in Navigation Bar. How to?


drayarms

Recommended Posts

Hello, I tried to give a shot at the problem posed in the subject line.  So below is my HTML and jQ for what I'm trying to accomplish.

 

html

 

				<ul class="topnav1">
    					<li class="highlight"><a href="index.php">Home</a></li>

    					<li><a href="testimonials_page.php">Testimonials</a></li>

    					<li><a href="services.php">Services</a></li>

    					<li><a href="contact_page.php">Contact Us</a></li>

				</ul>

 

jQ

 

 

	<script type='text/javascript'>



//Menu highlights.


		$(document).ready(function(){

      				var pathname = (window.location.pathname.match(/[^\/]+$/)[0]);


$(".topnav1 li a").each(function() {


				if ($(this).attr('href')==pathname) {


$("li.highlight").removeClass("highlight");


$(this).parent().parent().addClass("highlight");
}


				});


$("li.highlight"').parents().each(function(){
if ($(this).is("li")){
$(this).addClass("highlight");


				}


			});


		});





	</script>

 

 

The idea is to remove the highlighted class from its default list item, and assign it to the list item whose href attribute matches the current url.  I must admit I'm not the best at programming matching patterns, so I'm kind of at a loss as to how to match only part of the url with the href attribute and I'm not sure that's why my code isn't working( the highlight is retained on the home menu item and isn't applied to the others). Any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.