I am trying to finish up my navigation for my site. I'm attaching the jsfiddle code to show you what code I have now. My problem is my child links become gray when they are suppose to but, I want to make the top level link when I click on that gray as well. The way I have my pages labeled is like this
Page1
Page1a
Page1b
Page2
Page2a
.
.
.
ETC.
I need Page1 and Page2 to turn gray like the sublevels do. If anyone can help me with this I would really appreciate it. Thank you for your time.
<script type="text/javascript">
$('#body').ready(function(){
var URL = location.pathname.split("/");
URL = URL[URL.length-1];
//<![CDATA[
for(var i = 0; i < 11; i++){ // 4 = number of items, if you add more increase it, make number 1 larger than total items.
if ((URL.indexOf(i) != -1) && (!$('#i'+i).is(':visible'))) {
$('#nav ul:visible').slideUp('normal');
$('#i'+i).slideDown(0);
$('#i'+i)
.find('li')
.each( function() {
var current = $(this).find('a')[0];
if (current.href == window.location.href)
current.style.backgroundColor = "#ccc";
current.style.color = "#006";
});
}
}
});
</script>
Unfortunately none of the answers below have solved my issue, some have made it so the parent link now highlights, but it makes the other features not work correctly. I need the menu to still highlight in yellow when I hover over everything, I need the submenus to still be the light blue when not active, and I need all active links(parent or child) to show the gray highlight that they are the active link. Does anyone know the solution to fix all those issues?
Answer for this problem can be found at this post... Active Link on javascript menu to work on parent link not just child link
i am pretty sure that your control logic that decides in which menu item will be re-colored at what time needs some review, therefore i only changed the code to actually change the colors below. here is the fiddle that demonstrates the effect hard-coded on menu item #2.
note that if
location.pathname
ends with a/
you probably want refer to the elementURL[URL.length-2]
instead ofURL[URL.length-1]
.I redid your jQuery as it seemed overly complicated. Take a look at the result on this jsfiddle and let me know if that's what you were going for!
http://jsfiddle.net/PBKxy/
you can do it like this: http://jsfiddle.net/gUmYP/16/