Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: Adding selected CSS class to ActionLink if url matches current url

What is the simplest way of adding a selected css class to an ActionLink if the url of the action link matches the page being displayed?

Thanks!

like image 884
Sergio Avatar asked Nov 05 '22 23:11

Sergio


1 Answers

This might do the trick

var currentUrl = location.pathname; //spits out something like 'Home/About'

$('a').each(function() {
    if ($(this).attr('href') == currentUrl) {
        $(this).addClass('yourClassName');
    }
});
like image 73
tobias86 Avatar answered Nov 11 '22 15:11

tobias86