+ 1

Why this is code not working in here

https://code.sololearn.com/Wa8a4A6a20A4/?ref=app

2nd Jun 2021, 10:52 AM
Dhananjay Chaudhary
Dhananjay Chaudhary - avatar
2 Answers
+ 2
Because the javascript is loaded before the html document, and you are using methods in your code to select html elements that have not yet been loaded. You need to use the onload event to wait until the html document is fully loaded: onload =()=>{ Your code }
2nd Jun 2021, 11:03 AM
Solo
Solo - avatar
+ 2
Dhananjay Chaudhary, You can use event listeners to solve this problem: window.addEventListener('DOMContentLoaded', (event) => { //Put your JS code here }); Read more here: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
2nd Jun 2021, 12:13 PM
Isaac Fernandes
Isaac Fernandes - avatar