Sunday, June 2, 2024
45
rated 0 times [  48] [ 3]  / answers: 1 / hits: 43978  / 8 Years ago, tue, june 7, 2016, 12:00:00
<div id=par>
<span id=a1></span>
<span id=a2></span>
<div id=par2>
<span id=a3></span>
<span id=a4></span>
</div>
</div>

<script>


var ele = document.querySelectorAll('#par span');

for( var p of ele ){
console.log(p);
}


</script>


When i run this code I see error




Uncaught TypeError: ele[Symbol.iterator] is not a function




How to fix this problem ?


More From » ecmascript-6

 Answers
12

Convert NodeList to Array for make it in iterable form, use Array.from() to convert it.





<div id=par>
<span id=a1></span>
<span id=a2></span>
<div id=par2>
<span id=a3></span>
<span id=a4></span>
</div>
</div>


<script>
var ele = document.querySelectorAll('#par span');

for (var p of Array.from(ele)) {
console.log(p);
}
</script>








Refer the following question for more info : Are HTMLCollection and NodeList iterables?


[#61871] Sunday, June 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jayden

Total Points: 108
Total Questions: 109
Total Answers: 107

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
;