Created
April 1, 2024 08:29
-
-
Save nwebpro/596680733c9b3db4089b31bcabd397e2 to your computer and use it in GitHub Desktop.
Header Active Link In JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Active Link | |
let currentPageURL = window.location.href; | |
// Get all menu items | |
let menuItems = document.querySelectorAll('#mainNavigation ul li a'); | |
// Loop through each menu item to find the active page | |
for (var i = 0; i < menuItems.length; i++) { | |
if (menuItems[i].href === currentPageURL) { | |
menuItems[i].classList.add('active'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment