jQuery - How to Add Active Class to Current link matching Page URL? - Code By MavenBoys

Latest

Web Development & Blogging Tips/Tricks from 0 Level to Pro Level.

BANNER 728X90

Tuesday, 31 December 2019

jQuery - How to Add Active Class to Current link matching Page URL?

jQuery - How to Add Active Class to Current link matching Page URL?


Method 1 - On Page Load

If you want to add a class (active) to current link matching page URL on page load. Here is the working code

jQuery('.ma-menu-list li a[href="' + window.location.href + '"]').addClass('active');

Method 2 - On Link Click

If you want to add a class (active) to current link while clicking on the link. Here is the working code
jQuery('.ma-menu-list li a').on('click', function(){
jQuery('.ma-menu-list li a').removeClass("active");
jQuery(this).addClass("active");
});

No comments:

Post a Comment