I have the following HTML menu:
<div class="nav">
<ul>
<li class="first"><a href="/">Home</a></li>
<li><a href="/something/">Something</a></li>
<li><a href="/else/">Else</a></li>
<li class="last"><a href="/random/">Random</a></li>
</ul>
<ul style="float:right;">
<li class="first last"><a href="/news/">News</a></li>
</ul>
</div>
</div>
And then I have this code:
jQuery(function($){
var current = location.pathname;
$('.nav ul li a').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
})
The code is working great, but it has a problem. For example, if I see the Home page (www.example.com) then all of the menu links receives the active class. Another problem would be that it works great for www.example.com/something but it doesn't keep it active if I go to www.example.com/something/1 etc. Any idea how to fix this? Thanks in advance!