Created
December 18, 2018 17:47
-
-
Save tieutantan/be3e1db7a815727862e2dbb028b5f608 to your computer and use it in GitHub Desktop.
Add Active Navigation Class Based on URL
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
<nav> | |
<ul> | |
<li><a href="http://domain.com/">Home</a></li> | |
<li><a href="http://domain.com/about/">About</a></li> | |
<li><a href="http://domain.com/clients/">Clients</a></li> | |
<li><a href="http://domain.com/contact/">Contact Us</a></li> | |
</ul> | |
</nav> |
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
$(function() { | |
var current_url = window.location.href.split('?')[0]; // get current URL without query string | |
$('nav a[href^="' + current_url + '"]').parent().addClass('active'); // add class "active" to <li> | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment