Why this code is showing getContext () is error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why this code is showing getContext () is error

It is showing cannot read getContext () of null . https://code.sololearn.com/W6mrXACbXQi9/?ref=app

18th Aug 2020, 11:42 AM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
6 Answers
+ 3
https://www.sololearn.com/post/90825/?ref=app
18th Aug 2020, 11:58 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 3
On sololearn, code written in javascript tab runs before the code in HTML tab. As a result JS tries to access HTML DOM before it exists. As DOM is not ready `document.querySelctor` returns `null`. You can try this solution, Wrap code within a callback function to `load` event as below : window.addEventListener('load',function(){ //code here. }); Or, assign a function to `window.onload` property: window.onload=function(){ //code here }; There is also third way. Write in script tag below body tag . . . </body> <script> //code here </script> . . . But I'll suggest don't use this last method.You'll not get code highlighting. First method is best.
18th Aug 2020, 12:54 PM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 2
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ , Have you read the post that I linked in previous answer?
18th Aug 2020, 12:25 PM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 1
Could you explain clearly, where I have did mistake..
18th Aug 2020, 12:05 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
0
Yes,but I am confusing
18th Aug 2020, 12:36 PM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar
0
Tq
19th Aug 2020, 2:14 AM
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€
๐ŸŽถ๐Ÿ’žSravs๐Ÿ’ž๐Ÿฅ€ - avatar