Add ".active" class to the current page's link in a menu using jQuery or PHP [closed]
I need to add .active
class on active task's menu link on the page. I am working on localhost, here are few example URLs:
http://localhost/webapp/index.php?task=validate
http://localhost/webapp/index.php?task=register
and the structure of the menu:
<div class="menu">
<ul class="sf-menu">
<li><a href="index.php">Inicio</a></li>
<li><a href="?task=validator">Validar</a></li>
<li><a href="?task=register">Registro</a></li>
</ul>
</div>
You can use jQuery to loop through all links and compare their URLs with location
. Here is an example:
$(".sf-menu a").filter(function(){
return this.href == location.href.replace(/#.*/, "");
}).addClass("active");