Cant set property textContent of null | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Cant set property textContent of null

Hello, it happens me a lot here when I use a textContent or innerHTML property that I get an error saying uncaught typeError: can't set textContent / innerHTML property of null. var hello = document.getElementById("hello") ; hello.textContent = "Sololearn" ; I get an error and cant output it, while when I try it on the desktop I can do It without problems. Why is that?

5th Sep 2020, 7:44 AM
David
David - avatar
6 Answers
+ 2
David , you should add in the beginning of JS window.onload. Look at the code and hope it helps you 🐱 https://code.sololearn.com/Wb6y6vXxMp7N/?ref=app
5th Sep 2020, 8:25 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
David , can you show the whole code linked to Playground, so somebody can help you.
5th Sep 2020, 8:07 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
5th Sep 2020, 8:13 AM
David
David - avatar
0
Lol, I knew it was something funny. I've never even used onload function. Thanks a lot for help!
5th Sep 2020, 8:32 AM
David
David - avatar
0
David , you are welcome. It's because you should first load all html content before you use it, when it is in separate JS file.
5th Sep 2020, 8:35 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Ohh.. I never paid attention to this... This is obviously very important... Thanks a lot :)
5th Sep 2020, 8:36 AM
David
David - avatar