why is this throwing an error in js? var output =document.querySelector("#output"); output.innerHTML="Hello world"; reference error of output.innerHTML null line 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

why is this throwing an error in js? var output =document.querySelector("#output"); output.innerHTML="Hello world"; reference error of output.innerHTML null line 2

29th Sep 2016, 2:27 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
4 Answers
+ 4
declare this script code inside body ,otherwise declare this code inside window.onload=function(){} link:http://www.sololearn.com/app/javascript/playground/WrbhZ0C6NDmm/
29th Sep 2016, 6:10 PM
Moorthi R
Moorthi R - avatar
+ 1
the window.onload=function (){ some code} works. I Don' t understand the declaration in body though. do you mean dumping the js script in the html body part?
29th Sep 2016, 9:00 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
+ 1
yes correct as your are accessing Id from html code so the script code should run after the dom elements loaded
29th Sep 2016, 9:06 PM
Moorthi R
Moorthi R - avatar
+ 1
thanks very much for your help Moorthi
29th Sep 2016, 9:11 PM
Abdoulaye Seck
Abdoulaye Seck - avatar