I got following menu:
<ul id="nav" class="nav">
<li>
<a class="navitem active" href="javascript:loadTab();">My Profile </a>
</li>
<li>
<a class="navitem search" href="javascript:loadTab();"> Search </a>
</li>
<li>
<a class="navitem" href="javascript:loadTab();">Favorites </a>
</li>
</ul>
And my function (loaded in a .js file in the header):
function loadTab() {
jQuery(".navitem").removeClass("active");
jQuery(".navitem").click(function () {
jQuery(this).addClass("active");
});
}
Removing the class “active” works, adding the class “active” to the clicked element isn’t working. Any ideas?
Best regards!
As thatidiotguy said, you’re removing the class in the wrong place. You want something like this:
Edit: And also, no need for the
javascript:loadTab();in youratags. Change theirhrefattributes to#:Also, I added
event.preventDefault();in theclickevent handler for the links.