Does anyone know why console shows null? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does anyone know why console shows null?

I'm getting a weird error trying to get an element by its id, I checked the sintax over and over but still the same input: null. I really don't know if it's a sololearn playground error or what. Can anyone help me solve this pls https://code.sololearn.com/WblOZKSJKw4I/?ref=app

30th Dec 2019, 3:50 AM
Royner Perez
Royner Perez - avatar
5 Answers
+ 2
In your code, you have: window.onload = draw(); This won't work because it will set the onload property of to the result of the draw function. Remove the parentheses so the line looks like this: window.onload = draw; This sets window.onload to the actual function and not the result of it.
30th Dec 2019, 4:29 AM
Brian R
Brian R - avatar
+ 1
I think the problem is that the JavaScript is running before the DOM is loaded entirely, so the element you are trying to get doesn't exist when the code is run. To fix this, just run the code when the document is loaded. Do something like this: function run() { // ... code ... } window.onload = run;
30th Dec 2019, 3:55 AM
Brian R
Brian R - avatar
0
Can you link the code to this post so we can take a look?
30th Dec 2019, 3:51 AM
Brian R
Brian R - avatar
0
Brian R Thanks for answering, I followed your instructions and still the same input, I added some code to draw a line but the selector just won't work
30th Dec 2019, 4:22 AM
Royner Perez
Royner Perez - avatar
0
Brian R I get it now, thanks for your help, my code is working now
30th Dec 2019, 4:50 AM
Royner Perez
Royner Perez - avatar