0

Why isn't the array getting displayed?

First of all, I'm making a program and the array is really an important part of it and I wanted to display in the output to check that the array is working. Why is the array not getting displayed on the output? It also says that I have an error: Uncaught TypeError: Cannot set property 'innerHTML' of null This is my code: <p id="testing"> </p> var correct = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; document.getElementById('testing').innerHTML = correct;

23rd Sep 2020, 10:53 PM
:DD
:DD - avatar
3 Answers
+ 6
Hi Taehodz :D This happens because that JavaScript gets loaded before the HTML and since the HTML hasn't loaded yet the JavaScript can't get the paragraph by its ID that's why it's not working so I suggest you try this instead In the HTML <p id="testing"> </p> in the JavaSricpt window.onload = function(){ var correct = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; document.getElementById('testing').innerHTML = correct; }; Hope this helps and Good luck on your coding adventure ☕ đŸ©!
23rd Sep 2020, 11:32 PM
Vachila64☕
Vachila64☕ - avatar
+ 2
It doesn't matter tho Taehodz :D , Just put all your code inside that window.onload function We wouldn't have had to do that normally, but is cos of the way sololearn loads the codes!
24th Sep 2020, 9:22 AM
Vachila64☕
Vachila64☕ - avatar
+ 1
Thank you Vachila64! But how can I do this without the function? I have to use an array on another function...
24th Sep 2020, 12:06 AM
:DD
:DD - avatar