Getting or changing CSS class property with Javascript using DOM style

As mentioned by Quynh Nguyen, you don’t need the ‘.’ in the className. However – document.getElementsByClassName(‘col1’) will return an array of objects.

This will return an “undefined” value because an array doesn’t have a class. You’ll still need to loop through the array elements…

function changeBGColor() {
  var cols = document.getElementsByClassName('col1');
  for(i = 0; i < cols.length; i++) {

Leave a Comment