+ 2

Why this code works within script tag and not in js in code playground??

var obj; obj = document.getElementById("demo"); obj.innerHTML = " Hello";

7th May 2020, 7:43 AM
Tanweer Ahmed Ansari
Tanweer Ahmed Ansari - avatar
6 Answers
+ 5
onload = function(){ //your code here }
7th May 2020, 7:47 AM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar
+ 5
That is because Sololearn by default links out script in head tag . And because of that html elements are not loaded, so you can not select them instead you have to wait for them to load. You have to use onload event described by Mr. Sami Khan
7th May 2020, 7:55 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
could you please link the code here in this question?
7th May 2020, 7:57 AM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar
+ 2
Okay, i got it now,... Thank you both Sami Khan and šŸ”« Rick Grimes
7th May 2020, 7:59 AM
Tanweer Ahmed Ansari
Tanweer Ahmed Ansari - avatar
+ 1
Hmm, Nice explanation šŸ”« Rick Grimes , Deep_Blue
7th May 2020, 8:06 AM
Tanweer Ahmed Ansari
Tanweer Ahmed Ansari - avatar