Skip to content

Instantly share code, notes, and snippets.

@tieutantan
Created December 18, 2018 17:47
Show Gist options
  • Save tieutantan/be3e1db7a815727862e2dbb028b5f608 to your computer and use it in GitHub Desktop.
Save tieutantan/be3e1db7a815727862e2dbb028b5f608 to your computer and use it in GitHub Desktop.
Add Active Navigation Class Based on URL
<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>
$(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