Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using forEach on an array from getElementsByClassName results in “TypeError: undefined is not a function”

In my JSFiddle, I’m simply trying to iterate over an array of elements. The array is non-empty, as the log statements prove. Yet the call to forEach gives me the (not so helpful) “Uncaught TypeError: undefined is not a function” error.

I must be doing something stupid; what am I doing wrong?

My code:

var arr = document.getElementsByClassName('myClass');  console.log(arr);  console.log(arr[0]);  arr.forEach(function(v, i, a) {    console.log(v);  });
.myClass {    background-color: #FF0000;  }
<div class="myClass">Hello</div>
like image 619
Jer Avatar asked Jun 17 '14 14:06

Jer


People also ask

Is getElementsByClassName an array?

The getElementsByClassName() method returns an array-like of objects of the child elements with a specified class name. The getElementsByClassName() method is available on the document element or any other elements. The method returns the elements which is a live HTMLCollection of the matches elements.

Does getElementsByClassName return NodeList?

The getElementsByClassName() method returns a collection of child elements with a given class name. The getElementsByClassName() method returns a NodeList object.

How do I iterate an array of objects in Node JS?

forEach() is an array function from Node. js that is used to iterate over items in a given array. Parameter: This function takes a function (which is to be executed) as a parameter. Return type: The function returns array element after iteration.

Does getElementsByClassName return in order?

getElementsByClassName("a") will reliably list them in order: d1, d2, d3, d4, d5.


2 Answers

That's because document.getElementsByClassName returns a HTMLCollection, not an array.

Fortunately it's an "array-like" object (which explains why it's logged as if it was an object and why you can iterate with a standard for loop), so you can do this :

[].forEach.call(document.getElementsByClassName('myClass'), function(v,i,a) { 

With ES6 (on modern browsers or with Babel), you may also use Array.from which builds arrays from array-like objects:

Array.from(document.getElementsByClassName('myClass')).forEach(v=>{ 

or spread the array-like object into an array:

[...document.getElementsByClassName('myClass'))].forEach(v=>{ 
like image 145
Denys Séguret Avatar answered Oct 08 '22 03:10

Denys Séguret


Try this it should work :

<html>   <head>     <style type="text/css">     </style>   </head>   <body>    <div class="myClass">Hello</div>    <div class="myClass">Hello</div>  <script type="text/javascript">     var arr = document.getElementsByClassName('myClass');     console.log(arr);     console.log(arr[0]);     arr = [].slice.call(arr); //I have converted the HTML Collection an array     arr.forEach(function(v,i,a) {         console.log(v);     }); </script>   <style type="text/css">     .myClass {     background-color: #FF0000; } </style>    </body> </html> 
like image 33
Vaibhav Jain Avatar answered Oct 08 '22 03:10

Vaibhav Jain