Why the innerHTML property doesn't work.??! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why the innerHTML property doesn't work.??!

When i put the innerHTML property in javascript code.. The console of google says (Uncaught TypeError: Cannot set property ' innerHTML ' of null at <document.name> )... I want to know what's the reason and how can i repair the error..???!

13th May 2019, 4:15 PM
Ahmed Mohamed Zanaty
Ahmed Mohamed Zanaty - avatar
5 Answers
+ 4
This mostly happens when you run JS script before HTML loads. Provide your code so that we can clearly help...
13th May 2019, 4:26 PM
Kartik
Kartik - avatar
+ 2
https://www.sololearn.com/post/99071/?ref=app You can see an image to my code and problem on this post..
13th May 2019, 10:18 PM
Ahmed Mohamed Zanaty
Ahmed Mohamed Zanaty - avatar
+ 2
Kartik Krishnan It does't work. It says (x.innerHTML isn't a function in window.onload ) I don't know what's the error and where???!
14th May 2019, 11:09 AM
Ahmed Mohamed Zanaty
Ahmed Mohamed Zanaty - avatar
+ 1
Alright! So, the JS shouldn't be like that. First, you need to let the HTML load. So, do something like this in the JS file. window.onload = function() { //Your code here }; This runs your JS file only when the window is loaded, i.e. the HTML is done loading along with CSS. Let me know if it works now!
14th May 2019, 2:53 AM
Kartik
Kartik - avatar
0
Show the updated code. It should work
14th May 2019, 11:53 AM
Kartik
Kartik - avatar