I need to change the background for the menu item of the active page. I trying to achieve this with JQuery and CSS.
My code is: HTML
<ul class="nav">
<li id="header"><a href="Default.aspx">Home</a> </li>
<li>|</li>
<li><a href="About.aspx">About</a> </li>
</ul>
CSS
.nav li.active {
background-color:green;
}
.nav li.active a {
color:#fff;
font-weight:bold;
background-color:green;
}
And finally the JQuery I am using for this is as below.
$(function () {
setNavigation();
/* below statement is just to verify if jquery working at all*/
$('#header').css("background-color", "green");
});
function setNavigation() {
var path = window.location.pathname;
path = path.replace(/\/$/, "");
path = decodeURIComponent(path);
$(".nav a").each(function () {
var href = $(this).attr('href');
if (path.substring(0, href.length) == href) {
$("a").closest('li').addClass('active');
/* below statement is just to verify if jquery working*/
$('#header').css("background-color", "green");
}
});
}
I tried to debug this is using firebug still couldn't find the mistake. I'm a newbie to JQuery and This is not working (might be a silly mistake) :( Can anyone please help me with this?
ifstatement to verify that the condition is being met.