Last active
June 11, 2018 15:17
-
-
Save thebouv/8e6dbd6063c694139f07 to your computer and use it in GitHub Desktop.
Highlight active menu item in bootstrap nav (that is just a link and not a pill or tab)
This file contains 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
// highlight active menu item | |
var url = window.location; | |
$('ul.nav a').filter(function() { | |
return this.href == url; | |
}).parent().addClass('active'); | |
// alternative for use with specific parameter checking | |
var url = window.location; | |
$('ul.nav a').filter(function() { | |
locationLink = getURLParameter('calID',url); | |
menuLink = getURLParameter('calID',this.href); | |
return (locationLink == menuLink); | |
}).parent().addClass('active'); | |
function getURLParameter(name,url) { | |
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url)||[,""])[1].replace(/\+/g, '%20'))||null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will also filter on pills and tabs. To target only the navbar you should be more specific with the identifier like:
$('nav ul.navbar-nav a')