JavaScript error... help me pls | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript error... help me pls

Pls help me tO solve this problem... When i use forEach function on array it works but when i use forEach function on Dom manipulation like const img = document.querySelectorAll(“img”); it doesnt work it says img.forEach is not a function.. please help me tO solve this problem. Sorry for my english

25th Feb 2019, 7:58 AM
Stefenn
Stefenn - avatar
8 Answers
+ 11
NodeList is not an Array! It's just iterable but does not implement the same interface. Do: NodeList.prototype.forEach = Array.prototype.forEach; //global! or Array.from(imgVar); //recommended, use Array() for old ES
25th Feb 2019, 8:07 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
1. You need to put the scripts in window.onload so the DOM exists when you want to find them: https://code.sololearn.com/W5r7blSdpbqE/?ref=app 2. forEach is a new syntax and does not work for old browser. Check browser compatibility : https://caniuse.com/#search=forEach
25th Feb 2019, 11:42 AM
Gordon
Gordon - avatar
25th Feb 2019, 10:44 AM
Gordon
Gordon - avatar
+ 1
https://code.sololearn.com/WKZm8ny4AmkI/?ref=app
25th Feb 2019, 11:19 AM
Stefenn
Stefenn - avatar
0
yeah maybe i’ll use normal foor looping
25th Feb 2019, 11:17 AM
Stefenn
Stefenn - avatar
0
i’ve just tried forEach on SoloLearn and its work. but when i try forEach on my laptop its said forEach is not a function Haha
25th Feb 2019, 11:18 AM
Stefenn
Stefenn - avatar
0
why it doesn’t work on my laptop.......
25th Feb 2019, 11:20 AM
Stefenn
Stefenn - avatar
0
Ok bro i’ll try it later on my laptop... thankyou for your feedback bro
26th Feb 2019, 12:51 AM
Stefenn
Stefenn - avatar