Append a class to an existing selected class (selected class statement has multiple classes selected) in Javascript

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");

20 thoughts on “Append a class to an existing selected class (selected class statement has multiple classes selected) in Javascript”

Leave a Comment