Created
October 15, 2021 12:06
-
-
Save fernandovaller/5be35f08d64c7775eac2f79ce6ce46b7 to your computer and use it in GitHub Desktop.
Add class active for menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exemplo de como adicona a class CSS active no link que estiver sendo acessado | |
$(document).ready(function() { | |
// Get path | |
let uri = (window.location.pathname).split('/'); | |
// Option 1 | |
$('nav .nav-link').filter(`a[href="/admin/${uri[2]}"], a[href="/${uri[1]}"]`).addClass('active'); | |
// Option 2 | |
$('nav .nav-link').each(function(i, e) { | |
if ($(e).attr('href') === `/${uri[1]}`) { | |
$(e).addClass('active'); | |
return false; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment