+ 1

Unidentified error...

Hi, I was writing one of my first web codes on SoloLearn. I had to use the document.getElementById() function but I am facing a problem... This function actually returns a Null everytime I try it... I tried to find out what was wrong, and I looked at some other codes but I can't figure out what's the problem in what I wrote... In advance, thanks for your help! I join the code to this post : https://code.sololearn.com/W03IwW64Q698/?ref=app

17th Aug 2018, 10:27 PM
Kant1
Kant1 - avatar
2 odpowiedzi
+ 2
your code return null because you'd be calling the document before it was loaded,you need to wrap it in an window.onload function: window.onload = function() { var div = document.getElementById('myDiv'); alert(div); };
17th Aug 2018, 10:42 PM
MO ELomari
MO ELomari - avatar
+ 1
Ok thanks for your answer! If I want to make a bigger code, does it need to be all wrapped into this function? And is it a specification of SoloLearn or is it a function that I shall reuse in all my codes? Thanks!
18th Aug 2018, 8:56 AM
Kant1
Kant1 - avatar