Can someone point out the error in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Can someone point out the error in this code

I don't know why getting error in this code someone help me (btw I think its very small error) *HTML CODE* <span id="page2"> <h1><div id="rocktxt2">Rock</div><div id="papertxt2">Paper</div><div id="scissorstxt2">Scissors</div>THE GAME!</h1> </span> *CSS CODE* #rocktxt2{ color: #A8A8A8; } #papertxt2 { color: #dcdcdc; } #scissorstxt2 { color: red; } *javascriptcode* var page2 = document.getElementById("page2").style.display = "none";

8th May 2017, 12:01 PM
_Retr0/-
_Retr0/- - avatar
18 Answers
+ 10
....Doesn't relate to window.onload.... It's <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <span id="page2"> <h1><div id="rocktxt2">Rock</div><div id="papertxt2">Paper</div><div id="scissorstxt2">Scissors</div>THE GAME!</h1> </span> <script> var page2 = document.getElementById("page2").style.display = "none"; </script> </body> </html> The problem is SL auto include <script> in <head> tag While we need to include <script> in <body> tag You know what to do right?
8th May 2017, 12:42 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 10
@Calvin 😊 Yep window.onload Help a lot Bc <head> activate the javascript while element isn't loaded the page finished yet But window.onload....after loaded finished now. => activate the function inside window.onload
8th May 2017, 1:25 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
8th May 2017, 12:07 PM
_Retr0/-
_Retr0/- - avatar
+ 5
still the error persists :-( help!
8th May 2017, 12:12 PM
_Retr0/-
_Retr0/- - avatar
+ 5
no wait its not what u think it is
8th May 2017, 12:15 PM
_Retr0/-
_Retr0/- - avatar
+ 5
I want to make them disappear and am getting this error Unable to get property 'style' of undefined or null reference Line: 10
8th May 2017, 12:16 PM
_Retr0/-
_Retr0/- - avatar
+ 5
Even I think so
8th May 2017, 12:23 PM
_Retr0/-
_Retr0/- - avatar
+ 5
@ Calvin putting it in window onload makes this part of code work but the bigger part dosent function
8th May 2017, 12:25 PM
_Retr0/-
_Retr0/- - avatar
+ 5
idk without onload function my code runs perfect but I decided to edit it since then I an receiving this error
8th May 2017, 12:26 PM
_Retr0/-
_Retr0/- - avatar
+ 5
alright guys if alright try putting window onload in my code rock paper scissor s
8th May 2017, 1:30 PM
_Retr0/-
_Retr0/- - avatar
8th May 2017, 1:32 PM
_Retr0/-
_Retr0/- - avatar
+ 2
window.onload = function(){ // put your js code here. }
8th May 2017, 12:19 PM
Calviղ
Calviղ - avatar
+ 2
Your code is correct. There is a bug in Sololearn Playground. Refer to this JSFiffle link. https://jsfiddle.net/3nkschL3/ Note If you comment js codes. You will see that js was making span invisible.
8th May 2017, 12:24 PM
Ani Naslyan
Ani Naslyan - avatar
+ 1
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <span id="page2"> <h1><div id="rocktxt2">Rock</div><div id="papertxt2">Paper</div><div id="scissorstxt2">Scissors</div>THE GAME!</h1> </span> <style> #rocktxt2{ color: #A8A8A8; } #papertxt2 { color: #dcdcdc; } #scissorstxt2 { color: red; } </style> <script> var page2 = document.getElementById("page2").style.display = "BLOCK"; </script> </body> </html>
8th May 2017, 12:07 PM
Calviղ
Calviղ - avatar
+ 1
Change javascript code style display from "none" to "block"
8th May 2017, 12:14 PM
Calviղ
Calviղ - avatar
+ 1
Hi @Ani Naslyan Jsfiddle already set the js code to onload type by default, whereas SL code playground, we have to set it by ourself. This is not a bug on SL code playground.
8th May 2017, 12:25 PM
Calviղ
Calviղ - avatar
+ 1
Whenever you use document object, you have to set it inside window.onload, this function make sure that your elements are loaded before setting your element variables.
8th May 2017, 12:30 PM
Calviղ
Calviղ - avatar
+ 1
@Very You are right, when we put js code together with html, got to include script tag. But if the js code in js column, we have to put the code inside window.onload, whenever declare element variables. Many beginners make this mistake due to not familiar with SL code playground structure.
8th May 2017, 12:49 PM
Calviղ
Calviղ - avatar