Hide submenus when I scroll page

One tiny addition is that, unlike getElementsByClassName, querySelectorAll returns a NodeList, and NodeList objects have a forEach built in. This means spreading to an array is not needed if you need forEach (note that other array methods like map are not built in, so you may want to spread then, or manually call map on the list via call()).

addEventListener(
    'scroll',
    _ => document.querySelectorAll('.menu-submenu--show')
        .forEach(
            e => e.classList.remove('menu-submenu--show')
        )
)

getElementsByClassName return HTMLCollections which do not have a forEach. They are, like NodeLists, array-like, though, so you can ultimately use array methods on them (more on that in the array-likes tip of the day: JavaScript Tips of the Day ).

1 Like