mvc

Add active class to target link in jQuery

A
Desh-Duniya Team Author
| Published May 15, 2019
$(document).ready(function ($) {
            // Get current path and find target link
            var path = window.location.pathname.split("/").pop();
            // Account for home page with empty path
            if (path == '')
            {
                path = 'Home';
            }
            //var target = $('nav a[href="' + path + '"]');
            // Add active class to target link
            console.log(location.pathname);
            $('nav a[href^="' + location.pathname + '"]').addClass('active');
            //$('div ul li a[href^="/' + location.pathname.split("/")[1] + '"]').addClass('active');
           // target.addClass('active');
        });

Discussion (0)

Please sign in to participate in the discussion.
No comments yet. Be the first to join the conversation!