I have the following include file that creates a menu for my html pages:
function menu(){
document.write("<nav>");
document.write('<ul class="nav nav-pills pull-right dynamic-highlight">');
document.write('<li role="presentation"><a href="index.html">List Widgets</a></li>');
document.write('<li role="presentation"><a href="create_widget.html">Create Widget</a></li>');
document.write('<li role="presentation"><a href="create_group.html">Create Widget Group</a></li>')
document.write('<li role="presentation"><a href="department_widget_list.html">Department Widget List</a></li>')
document.write('<li role="presentation"><a href="group_membership_list.html">Group Members List</a></li>')
document.write('<li role="presentation"><a href="site_conf.html">Site Configuration</a></li>')
document.write('</ul>')
document.write('</nav>')
}
Each page that includes this menu has code like this:
<head>
... stuff....
<script type="text/javascript" src="menu_html.js"></script>
</head>
<body>
<div class="container"><br>
<div class="header clearfix">
<script type="text/javascript">
menu();
</script>
I've added the following javascript to try find all "
$(document).ready(function() {
var loc = window.location.pathname;
loc = loc.substring(1);
console.log(loc);
$('.dynamic-highlight').find('a').each(function() {
console.log($(this).attr('href'))
$(this).addClass('active', $(this).attr('href') == loc);
//$(this).addClass("active");
});
Using the console output, I can see that the current location's page name matches items return by the find method. But the addClass doesn't seem to be working because I don't end up with a highlighted menu item. I've also tried .toggleClass instead of addClass.
EDIT 1
I don't get any errors, but there's no highlighting that appears. So I tried opening the F12 window and using the DOM Explorer, I tried to select the "
Just to make sure that "active" is the right class that i want, I hardcoded / changed my include to look like this :
document.write('<li role="presentation" class="active"><a href="index.html">List Widgets</a></li>');
and refresh my page. And I do see the "List widgets" menu item highlighted.
Not sure what else to try...