Highlight current menu item in JQuery
21 Aug 2016
Having issues highlighting your current navigation menu item? JQuery makes it easy in just a few lines of code. Here’s a copy-pasta for you.
If you have a standard menu/navigation layout like below, copy the stuff between the ‘script’ tags and stick it somewhere close to the your navigation element.
I initially had the script executing at the bottom of the page, however there was a noticeable flash when navigating through the pages.
All this does is go through each ‘a’ within my ‘nav’ element and if the URL of the link is equal to the current window URL, then it sets the id attribute to active.
Note: you will need to add some style for your new id tag e.g.