Skip to content

Instantly share code, notes, and snippets.

@Ibochkarev
Created August 28, 2015 18:35
Show Gist options
  • Save Ibochkarev/fdb0a5c4152e4870b9f4 to your computer and use it in GitHub Desktop.
Save Ibochkarev/fdb0a5c4152e4870b9f4 to your computer and use it in GitHub Desktop.
Twitter Bootstrap add active class to li
//Active
var url = window.location;
// Will only work if string in href matches with location
$('ul.navbar-nav a[href="'+ url +'"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.navbar-nav a').filter(function() {
return this.href == url;
}).parent().addClass('active');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment