getelementsbyclassname

JS: iterating over result of getElementsByClassName using Array.forEach

我怕爱的太早我们不能终老 提交于 2019-11-25 18:55:38
I want to iterate over some DOM elements, I'm doing this: document.getElementsByClassName( "myclass" ).forEach( function(element, index, array) { //do stuff }); but I get an error: document.getElementsByClassName("myclass").forEach is not a function I am using Firefox 3 so I know that both getElementsByClassName and Array.forEach are present. This works fine: [2, 5, 9].forEach( function(element, index, array) { //do stuff }); Is the result of getElementsByClassName an Array? If not, what is it? No. As specified in DOM4 , it's an HTMLCollection (in modern browsers, at least. Older browsers