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

12/30/2019 3:50:26 AM

Royner Perez

5 Answers

New Answer


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.


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;


Can you link the code to this post so we can take a look?


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


Brian R I get it now, thanks for your help, my code is working now