Highlight Current page in menu using Javascript
Posted July 13, 2010
on:- In: Scripts | Web Design
- 2 Comments
HTML:
<div id=”NavBar”>
<ul>
<li><a href=”home.html” id=”ctl00_toolbar_hlHome”>Home</a></li>
<li><a href=”services.html” id=”ctl00_toolbar_hlSearch”>Services</a></li>
<li><a href=”about.html” id=”ctl00_toolbar_hlProducts”>About</a></li>
<li><a href=”contact.html” id=”ctl00_toolbar_hlServices”>Contact</a></li>
</ul>
</div>
CSS:
#NavBar{height: 30px;margin-bottom:2px; border-bottom:2px solid black;}
#NavBar ul{display: inline;padding: 0;margin: 0; width: 960px;}
#NavBar li{list-style-type: none;line-height: 28px;vertical-align: middle;display: block;
text-transform: uppercase;font-size: 12px;float: left;margin-left: 0px;}
#NavBar ul li a{ background: darkred repeat-x;height: 28px;display: block;
text-decoration: none;text-align: center;color: #FFFFFF;margin-right:2px;
padding:0 25px; font-weight:bold; border:1px solid #990200;}
#NavBar ul li a:hover{background: black repeat-x;height: 28px;display: block;
color: #FFFFFF;text-decoration: none;border:1px solid #000000}
#NavBar ul li a.activeMenuItem {background: black; height: 28px;display: block;
color: #FFFFFF;text-decoration: none;border:1px solid #000000}
JavaScript:
Finally call this JavaScript to all pages:
<script language=”javascript”>setPage()< /script>
Source:http://www.richnetapps.com/automatically_highlight_current_page_in/
2 Responses to "Highlight Current page in menu using Javascript"
Very great post. I just stumbled upon your weblog and wished to mention that I’ve truly loved surfing around your weblog posts. After all I’ll be
subscribing for your rss feed and I am hoping you write again very
soon!
1 | Keith
November 13, 2011 at 5:35 pm
Howdy Kathirpositive,
Thanks for a great post and code. I’ve applied the code to my site’s menu, but have a question….
In the menu (which is entirely instead of ), I’m needing to highlight both a heading and a subheading. For example:
FRACTURES
forearm
upper arm
thigh
ankle
In this case as a person moves through the Fractures section, Fractures stays highlighted in addition to the submenu areas. So I would potentially need 2 areas highlighted.
If you have any insight into this challenge, or know of where you could point me for resources, I would be very grateful!
Kind Regards,
Keith