Skip to content

Instantly share code, notes, and snippets.

@fernandovaller
Created October 15, 2021 12:06
Show Gist options
  • Save fernandovaller/5be35f08d64c7775eac2f79ce6ce46b7 to your computer and use it in GitHub Desktop.
Save fernandovaller/5be35f08d64c7775eac2f79ce6ce46b7 to your computer and use it in GitHub Desktop.
Add class active for menu
// 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