How to get an active class from url

Hello. i have this menu code:

<!-- Responsive menu -->
<div class='rmm' data-menu-title = "Navigation" data-menu-style = "custom">
<ul>
<li><a href="./menu.html">HOME</a></li>
<li><a href="./menu2.html">CLIENTS</a></li>
<li><a href="./menu3.html">PROJECTS</a></li>
<li><a href="./menu4.html">CONTACT US</a></li>
<li><a href="./menu5.html">WEBSHOP</a></li>
<li><a href="./menu6.html">ADMIN</a>
</li>
</ul>
</div>

And then i got this .js code to show an active link color in a sparat file (activelink.js):

jQuery(document).ready(function($){
var url = window.location.href;
$(‘.rmm a’).filter(function() {
return this.href == url;
}).closest(‘li’).addClass(‘current_yes’);
});

and in the in the .js file that makes the menu function i have this:

some code…

var nav = $(this),
currentPageItem = $(‘.current_yes’, nav);

some more code…

And this works well, but now i’m working on another menu and the code for this menu looks like this:

<!-- Responsive menu -->
<div class="ribbon">
<div class="rmm style">
<ul>
<li>
<a href="">HOME</a>
</li>
<li>
<a href="">PROJECTS</a>
<ul>
<li>
<a href="">IKEA - NORWAY</a>
</li>
<li>
<a href="">FASTEC - SWEDEN</a>
</li>
</ul>
</li>
<li>
<a href="">CLIENTS</a>
<ul>
<li>
<a href="">IKEA</a>
</li>
<li>
<a href="">FASTEC</a>
</li>
</ul>
</li>
<li>
<a href="">CONTACT US</a>
</li>
<li>
<a href="">WEBSHOP</a>
</li>
<li>
<a href="">ADMIN</a>
<ul>
<li>
<a href="">STAFF</a>
</li>
<li>
<a href="">DOCUMENTS</a>
</li>
</ul>
</li>
</ul>
</div>
</div>

But the problem is that the code in the activelink.js file do not work for this. Any body know way? I have tested to replace the .rmm a to several other classes like: .ribbon a or .rmm.style etc.

Thank you.

Maybe because second menu has links with empty href attributes?

Hello and thank you for replying. But no, that’s not it. Even if i put in links it does not work. I set liks for index.html like this: ./index.html and for Clients ./clients. But no luck.

OK i made a small change to the code but now all the li are getting a class of current_yes not just the current one. I have this kode now:

jQuery(document).ready(function($){

var url = window.location.href;

$('.ribbon a').filter(function() {

return this.href == url;

}).closest('li').addClass('current_yes');

});

Never mind it works

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.