I'm trying to change the classname if certain links in my navigation, but the doesn't seem to want to iterate through the last item. The commented out hardcoded section works just fine
HTML
<ul id="nav-list">
<li class="not-current"><a href="home.html">Home</a></li>
<li class="current"><a
href="documentation.html">Documentation</a></li>
<li class="not-current"><a href="designs.html">Designs and
Wireframes</a></li>
<li id="last-nav-item" class="not-current"><a
href="meeting.html">Book a Meeting</a></li>
</ul>
JavaScript
var notCurrentLinks = document.getElementsByClassName('not-
current');
function openMobileMenu() {
for (let i = 0; i < (notCurrentLinks.length); i++) {
console.log('Worked ' + (i + 1) + ' time(s)');
notCurrentLinks[i].className = 'm-not-current';
}
//Hardcoding below seems to work
//notCurrentLinks[0].className = 'm-not-current';
//notCurrentLinks[1].className = 'm-not-current';
//notCurrentLinks[2].className = 'm-not-current';
notCurrentLinks = document.getElementsByClassName('m-not-
current');
}
The console returns
- Worked 1 time(s)
- Worked 2 time(s)