var li = document.querySelectorAll('nav ul li a'), index = 0, path = decodeURI(window.location.pathname); [...li].forEach((el, i) => path.match(el.getAttribute('href')) && (index = i)); li[index].classList.add('active');