John R Perry John R Perry - 1 year ago 68
CSS Question

jQuery add class .active on menu within subfolders

I was able to find a solid solution on how to add an 'active' class to menu items based on the url of the page:

var page = window.location.pathname,
find = new RegExp(page == '/' ? window.location.origin + '/?jQuery' : page.replace(/\/jQuery/,''));
jQuery('nav a').each(function(){

This, however, does not work with links within subfolders and I can't seem to figure out a way to make it work. What am I missing?

Answer Source

This is what I use and it works for menu items in my subfolders

$(".menu-item").each(function() {
  if (this.href == window.location.href) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download