Skip to content

Instantly share code, notes, and snippets.

@neutraltone
Last active January 14, 2016 00:08
Show Gist options
  • Save neutraltone/6304913 to your computer and use it in GitHub Desktop.
Save neutraltone/6304913 to your computer and use it in GitHub Desktop.
JavaScript to highlight current navigation item.
/**
* Active Nav Item
*/
// Get the URL
var url = window.location;
// Select nav list item and select
// Will only work if string in href matches with location
$('ul.nav a[href="'+ url +'"]').parent().addClass('selected');
// Will also work for relative and absolute hrefs
$('ul.nav a').filter(function() {
return this.href == url;
}).parent().addClass('selected');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment