Can someone assist me with looping over the number of nav-items I have? I am encountering an error that says: Property 'length' does not exist on type 'HTMLElement'. I understand that changing document.getElementById('nav-item) to document.getElementsByClassName('nav-item') would resolve the error, but I prefer to access the element through its id. Any suggestions on how to accomplish this?
const header = document.getElementById('applicationLinks');
const navItems = document.getElementById('nav-item');
for (let i = 0; i < navItems.length; i++) {
navItems[i].addEventListener('click', function () {
const current = document.getElementsByClassName('active');
current[0].className = current[0].className.replace('active', '');
this.className += ' active';
});
}
I am new to this, so any help you can provide would be greatly appreciated. Thank you!