[SOLVED] Dynamically adding highlight background on active navigation link in Webpage.
I want to add a highlight to a corresponding link in a navigation menu when the related page is open. And I tried the following code and it is working perfectly, but It seems lengthy. How can I do this in a easier, shorter and smarter way Code: let navLinks = document.querySelectorAll(".header .menu ul li"); /*navLinks structure: Home => /index.html or site name only About => /about.html Contact => /contact.html Signup => /join.html ....................*/ let linkPath = window.location.href; if(/localhost:8158$/.test(linkPath) || /localhost:8158\/index/.test(linkPath)) { navLinks[0].classList.add("active"); } if(/localhost:8158\/about/.test(linkPath)) { navLinks[1].classList.add("active"); } if(/localhost:8158\/contact/.test(linkPath)) { navLinks[2].classList.add("active"); }