I’m selecting multiple classes with one statement:
var sidebarList = document.getElementsByClassName("side-nav nav-links nav-item");
I’m trying to add a class called open
to it:
sidebarList.classList.add("open");
But I am getting this error Cannot read property 'add' of undefined
and I do not understand why.
This is the jquery equivalent of what I’m trying to do
var sidebarList = $(".side-nav ul li");
sidebarList.addClass("open");
You have to use querySelectorAll for selecting elements with multiple classnames as selector. After that you’ve to loop through your selected elements and add the class to each element.