Uncaught TypeError : cannot set properties of null (setting 'innerHTML') | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Uncaught TypeError : cannot set properties of null (setting 'innerHTML')

This code just work fine in Browser but it's showing error running on SoloLearn while clicking the Guess button Uncaught TypeError : cannot set properties of null (setting 'innerHTML') https://code.sololearn.com/W0Dsws4FND40/?ref=app

21st Mar 2022, 12:18 AM
Twaatii
Twaatii - avatar
7 Answers
+ 3
This script uses a call (document.querySelector(...)) to the DOM of an html-document that has not yet been loaded. Use onload method: onload = ()=>{code}
21st Mar 2022, 12:46 AM
Solo
Solo - avatar
+ 1
Subscribe to me and I will explain everything in detail to you in a personal 😉
21st Mar 2022, 2:18 AM
Solo
Solo - avatar
+ 1
Niirmaal Twaatii When you call function using event function then get input value inside the function don't get when code load otherwise you will get null and you cannot set value to null object. So write this lines inside update function var res = document.querySelector("#result"); var prev = document.querySelector("#prev"); if you want to use res and prev in another function then make them global variables then assign value Also you should get random value inside function because it should change on every click. If you write once outside the function then random value will be always 1 so write this line inside function: var ran = Math.floor(Math.random()*100 + 1); https://code.sololearn.com/WimrzhG5ohE8/?ref=app
21st Mar 2022, 2:39 AM
A͢J
A͢J - avatar
+ 1
A͢J, it's much easier than you think 😉 And "var ran = Math.floor(Math.random()*100 + 1);" you can not write inside the function as you suggest, as this will deprive the player of the next attempt that is counted.
21st Mar 2022, 3:01 AM
Solo
Solo - avatar
+ 1
A͢J thanks for your help 😃 & my program is to guess the number in 10 tires so no need to generate random number on every click 😉
21st Mar 2022, 3:40 AM
Twaatii
Twaatii - avatar
0
Solo can you specify which block of code to put on onload method because its getting reference error if if i put the whole code inside it 😅
21st Mar 2022, 12:57 AM
Twaatii
Twaatii - avatar
0
Solo okay
21st Mar 2022, 3:41 AM
Twaatii
Twaatii - avatar