Thursday, May 9, 2024
138
rated 0 times [  145] [ 7]  / answers: 1 / hits: 21851  / 8 Years ago, sun, december 25, 2016, 12:00:00

I've been working on JavaScript lately, and everything was fine until I opened my page in IE11. as per Mozilla website .forEach is supported from IE9.



This is the error I got.




SCRIPT438: Object doesn't support property or method 'forEach'




and this is the code.



var link1 = document.querySelectorAll(nav a);
var textbox = document.getElementById(OutputWindow);
link1.forEach(function (element) {
textbox.innerHTML += <br/> + element + n;
element.onclick = function () {
alert(Hello!);
console.log(hello!);
confirm(Hello!);
};
});


I tried polyfill, but to my amusement, Array has a forEach in IE11.



Then where I'm going wrong?



PS: This works fine in Chrome.


More From » internet-explorer

 Answers
95

Finally mystery solved.



Apparently, IE9 and above supports Array.forEach but not for NodeList, which querySelector returns. I tried Array.from() to no avail as it requires ES6 or use ES6-shim.



All I had to do was to convert from nodeList to Array, and I did by this.



Array.prototype.slice.call(document.querySelectorAll(nav a), 0);


as appeared in question In Javascript, what is the best way to convert a NodeList to an array


[#59570] Thursday, December 22, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
noa

Total Points: 579
Total Questions: 83
Total Answers: 93

Location: Sweden
Member since Mon, Aug 10, 2020
4 Years ago
;