+ 1

Why does not my code work as the the one provided??( in terms of printing the value 0 1 2 3 ...like that ....check the code pls)

https://code.sololearn.com/WqIZL5QGTT9i/?ref=app https://code.sololearn.com/W5wHuejAfLIL/?ref=app

4th Mar 2019, 7:49 PM
[ ]
[     ] - avatar
7 ответов
+ 6
[ ] That's because the JS is executed before the site is loaded. Copy the script and paste it between <script></script> tags at the end of your html file (before the </body> tag)
4th Mar 2019, 9:25 PM
Anna
Anna - avatar
+ 5
[ ] as explained by Anna, the JS is running before the DOM is actually loaded. Another way to manage this is to wrap your code in a function body which is then called when the document load event fires. window.onload = function(){ //Insert your current code here... }
4th Mar 2019, 9:42 PM
David Carroll
David Carroll - avatar
+ 3
It works if you change it from o=document.getElementsByClassName("g") to o=document.getElementById("g") and change the element's class to id
4th Mar 2019, 8:25 PM
Anna
Anna - avatar
4th Mar 2019, 10:06 PM
[ ]
[     ] - avatar
0
Anna when i do it by id its saying cant set 'innerHTML 'to null.... https://code.sololearn.com/WxRU3DJzQtAA/?ref=app
4th Mar 2019, 8:56 PM
[ ]
[     ] - avatar
0
[ ] Your new code (‘by id’) works for me.
4th Mar 2019, 9:01 PM
Russ
Russ - avatar
0
document.getElementsByClassName("g") returns an Array. So, you have to use o[0] in order to access your element: o[0].innerHTML = ...
5th Mar 2019, 3:25 AM
Микола Федосєєв
Микола Федосєєв - avatar