Iterating Over getElementsByClassName Results Using Array.forEach: A Comprehensive Guide

Nov 20, 2025 · Programming · 14 views · 7.8

Keywords: JavaScript | HTMLCollection | forEach | DOM Manipulation | ES6

Abstract: This article provides an in-depth analysis of the HTMLCollection object returned by document.getElementsByClassName in JavaScript, explaining its differences from Array objects and detailing three effective iteration methods: the compatible Array.prototype.forEach.call approach, ES6's Array.from conversion, and spread operator syntax, with comparisons to querySelectorAll usage scenarios.

Problem Background and Core Concepts

In JavaScript development, developers frequently need to manipulate collections of DOM elements. The document.getElementsByClassName("myclass") method is commonly used, but many developers mistakenly believe it returns an array that can be directly iterated using Array.forEach. According to the DOM4 specification, this method actually returns an HTMLCollection object (in modern browsers), while older browsers may return a NodeList object.

Differences Between HTMLCollection and Array

HTMLCollection is an array-like object that has a length property and allows element access via index, but it is not a true Array instance. Therefore, it lacks Array prototype methods such as forEach, map, and filter. This is why directly calling document.getElementsByClassName("myclass").forEach throws a "forEach is not a function" error.

Solution 1: Using Array.prototype.forEach.call

For scenarios requiring compatibility with older browsers, use the Array.prototype.forEach.call method, passing the HTMLCollection as the this value:

var els = document.getElementsByClassName("myclass");
Array.prototype.forEach.call(els, function(el) {
    // Process each element here
    console.log(el.tagName);
});

Alternatively, use the more concise array literal syntax:

[].forEach.call(els, function(el) {
    // Processing logic
});

This approach leverages JavaScript's call method to invoke array methods on array-like objects.

Solution 2: Using ES6's Array.from

In environments supporting ES6, use the Array.from method to convert the HTMLCollection into a true array:

Array.from(document.getElementsByClassName("myclass")).forEach((el) => {
    // Use arrow function to process elements
    console.log(el.tagName);
});

This method offers cleaner code by utilizing modern JavaScript features.

Solution 3: Using Spread Operator

ES6 also provides the spread operator ..., which can convert array-like objects into arrays:

[...document.getElementsByClassName("myclass")].forEach((element, index, array) => {
    // Processing logic with access to index and array parameters
});

The spread operator works on all array-like objects, providing an elegant conversion method.

Comparison with querySelectorAll

Another common DOM query method is document.querySelectorAll, which returns a NodeList object. While modern browsers' NodeList may support the forEach method, other array methods like map and filter are still missing. Thus, the above conversion methods are equally applicable:

[...document.querySelectorAll(".myclass")].map((element) => {
    return element.innerHTML;
});

This makes querySelectorAll more flexible when chaining array methods is required.

Compatibility Considerations

When choosing a solution, consider the target environment's compatibility:

Conclusion

Understanding the characteristics of the HTMLCollection object returned by getElementsByClassName is key to avoiding common errors. Effective iteration can be achieved via Array.prototype.forEach.call, Array.from, or the spread operator. In modern development, combining ES6 features allows for writing cleaner, more readable code while ensuring backward compatibility through transpilation tools.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.