Why is that my JavaScript code doesnot run as expected? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is that my JavaScript code doesnot run as expected?

This happens especially when I try manipulating html elements from JavaScript. Below is some example of code like that. Please help me find the problem. https://code.sololearn.com/WcpzYPFy62qs/?ref=app

24th May 2019, 6:14 AM
Agaba Edwin
Agaba Edwin - avatar
2 Answers
+ 3
It's because you declared the variable for the canvas before the page loaded, so it became undefined. You should do it like this: window.onload = () => { //your code here }
24th May 2019, 6:20 AM
Airree
Airree - avatar
0
Thank you Airree. It worked like a charm
24th May 2019, 8:16 AM
Agaba Edwin
Agaba Edwin - avatar