How do I add “active class” to the menu item with jQuery that links to the page on which the visitor or user is.
Last edited by Mati on September 21st 2015, 11:53 am; edited 1 time in total
$(function() {
var cleanURI = window.location.href.replace(/\?.*|#.*/, ''), // get the clean URL without queries or hashes
menu = $('a.mainmenu'), // get menu items
i = 0, // set index
j = menu.length; // cache menu length
// loop menu links
for (; i < j; i++) {
// clean the href and compare it with the window location
if (menu[i].href.replace(/\?.*|#.*/, '') == cleanURI) {
menu[i].className += ' activeNav'; // add a new class
break; // break out of the loop because we found our match
}
}
});
a.mainmenu.activeNav {
background:#39C;
}