Adding active class to menu link if corresponding page is active

How to add active class to menu link if corresponding page is active?

Script


$(function () {
    var current = location.pathname;
    $('.nav 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');
        }
    })
})

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *