getElementsByClassName()
returns an HTMLCollection
not an Array
. You have to convert it into a JavaScript array first :
allImgs = Array.prototype.slice.call(allImgs);
// or
allImgs = [].slice.call(allImgs);
// or (thanks @athari)
allImgs = Array.from(allImgs);
// or (thanks @eliaz-bobadilla)
allImgs = [...allImgs]