I\'m using jQuery and I\'m trying to add a class to a menu item based on URL. I tried this(found in other topics), but cannot get it to work properly. It adds the class to every
You need a break after each case or else it will just go on to the next one.
switch (window.location.pathname) {
case '/p/about.html':
$('.nav-about').addClass('current');
break;
case '/search/blog':
$('.nav-blog').addClass('current');
break;
case '/p/design.html':
$('.nav-design').addClass('current');
break;
case '/p/photography.html':
$('.nav-photography').addClass('current');
break;
case '/p/hosting.html':
$('.nav-hosting').addClass('current');
break;
}
Side point, this is repetitive:
$(document).ready(function(){
$(function() {
Both of those mean the same thing, use one of them.