As others have pointed out, the difference is getElementById
returns a single element, whereas getElementsByClassName
yields a collection of elements. On a single elements you can use .className
as it is a property of that element.
Now to answer your main question; in order to add class2
to all elements which have class1
you do this:
var elements = document.getElementsByClassName('class1');
for(i = 0; i < elements.length; i++) {
elements[i].className += ' class2';
}