Function to add a class

I’m trying to add a class to a bunch (15) elements by using this

function activateGPS() {

var systemsshadows = document.getElementsByClassName("shadow");
console.log(systemsshadows);

systemsshadows.classList.add( 'inactive-shadow' );
}

The console.log seems to work, But I get an error trying to add the class

I think I loop through the array like

for (i = 0; i < systemsshadows.length; i++) {
    systemsshadows[i].classList.add( 'inactive-shadow' );
}

Grace Hopper wrote the first compiler ever written, at least in this world. I wonder what she would say. I think she would encourage you to look closer.

The error message is saying that classList is undefined. You don’t show us what element the message is complaining about. Perhaps you can (in the code) just check if classList is undefined before doing the add; if it is undefined then it is probably not possible to use a CSS class on the element.

Note to others: look at the image to see why I mention Grace Hopper.

1 Like