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
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
+ 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
+ 3
Use normal for loop
https://code.sololearn.com/WJb8CtyOr84o/?ref=app
+ 1
https://code.sololearn.com/WKZm8ny4AmkI/?ref=app
0
yeah maybe iâll use normal foor looping
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
0
why it doesnât work on my laptop.......
0
Ok bro iâll try it later on my laptop... thankyou for your feedback bro