12th Sep 2021, 11:26 AM
EsaKurniawan
5 Answers
+ 2
let appear; onload=()=>{ appear = document.querySelector('#empty'); } function show() { appear.innerHTML = Date(); }
12th Sep 2021, 11:27 AM
Abhay
Abhay - avatar
+ 1
Abhay can you explain why my code doesn't work?
12th Sep 2021, 11:36 AM
EsaKurniawan
+ 1
EsaKurniawan sololearn keeps the javascript file in head ,therefore javascript is executed first and then the rest of html dom is loaded. Javascript execution isn't much of a problem but when you make dom related queries like these "let appear = document.querySelector('#empty'); " They fail since element with id empty doesn't exist yet . Onload function handler delays the execution of javascript by checking if the html dom is loaded which solves that problem.
12th Sep 2021, 11:49 AM
Abhay
Abhay - avatar
+ 1
Abhay thanks
12th Sep 2021, 11:53 AM
EsaKurniawan
0
<button onclick="this.textContent=new Date()">Show Hidden Date</button>
12th Sep 2021, 12:40 PM
SAN
SAN - avatar