Skip to content

Instantly share code, notes, and snippets.

@fhdalikhan
Created October 11, 2020 22:04
Show Gist options
  • Save fhdalikhan/95476ee0e6ab4d55a9ff50eb1b761642 to your computer and use it in GitHub Desktop.
Save fhdalikhan/95476ee0e6ab4d55a9ff50eb1b761642 to your computer and use it in GitHub Desktop.
ADD CLASS TO ACTIVE ANCHOR THROUGH JS
// ADD CLASS TO ACTIVE ANCHOR THROUGH JS
$(function() {
var url = window.location.href;
// Will only work if string in href matches with location
$('.Topnav a[href="'+ url +'"]').addClass('active');
// Will also work for relative and absolute hrefs
$('.Topnav a').filter(function() {
return this.href == url;
}).addClass('active');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment