It's because document.getElementsByClassName('class')
doesn't return an Array
, but a NodeList
which is an array-like object. You can turn an array-like object to an array by using:
var elements = document.getElementsByClassName('class');
var arr = Array.prototype.slice.call(elements);