+ 2

Javascript question: property

How to fix this error that appears when I use innerHTML or innerText in both of these. Uncaught TypeError: Cannot set property 'innerHTML' of null

2nd Oct 2020, 4:11 PM
Farhad Rahmani
Farhad Rahmani - avatar
17 Answers
2nd Oct 2020, 4:16 PM
Farhad Rahmani
Farhad Rahmani - avatar
+ 1
Farhad Rahmani just wrapped it into window.onload = () => { // Your code should be here }
2nd Oct 2020, 4:23 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
+ 1
๐ŸŽƒ#H P 22โ„ข๐ŸŽƒ I think. His code doesn't have any bug or error. But sololearn behaves differently from browser. When you use js tab. It is good practice to wrap your code in window.onload
2nd Oct 2020, 4:29 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
+ 1
Farhad Rahmani Did my code works?
2nd Oct 2020, 4:36 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
+ 1
Javascript loads line by line so when it hits the line with innerHTML it gives an error because it haven't gotten anything with that class or id so it's very simple to resolve it just add your script tag at the end of body tag ๐Ÿ™ŒHappy Coding ๐Ÿ™Œ
4th Oct 2020, 2:48 PM
๐Ÿ‘‘๐•ป๐–—๐–”๐–Œ๐–—๐–†๐–’๐–’๐–Š๐–—๐Ÿ‘‘
๐Ÿ‘‘๐•ป๐–—๐–”๐–Œ๐–—๐–†๐–’๐–’๐–Š๐–—๐Ÿ‘‘ - avatar
0
๐ŸŽƒ#H P 22โ„ข๐ŸŽƒ what is wrong in syntax?
2nd Oct 2020, 4:24 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
0
100 time i checked but that was perfectly correct.
2nd Oct 2020, 4:25 PM
Farhad Rahmani
Farhad Rahmani - avatar
0
Farhad Rahmani just wrap it into window.onload = () => { // Your code should be here }
2nd Oct 2020, 4:25 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
0
No i have coded in visual code but over here is also that kind of error.
2nd Oct 2020, 4:31 PM
Farhad Rahmani
Farhad Rahmani - avatar
0
2nd Oct 2020, 4:31 PM
Farhad Rahmani
Farhad Rahmani - avatar
2nd Oct 2020, 4:33 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
0
Vivek no bro sorry because over there have to shown day hour minute and seconds.
2nd Oct 2020, 4:38 PM
Farhad Rahmani
Farhad Rahmani - avatar
0
See one more time. do it works? https://code.sololearn.com/WN3BKy9a17Rn/?ref=app
2nd Oct 2020, 4:41 PM
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณVivek๐Ÿ‡ฎ๐Ÿ‡ณ - avatar