+ 1

JavaScript not working

For some reason, the functions to call HTML elements doesn't work, I compared to another codes, and seems to be equal, so I doesn't know why is not working :/ https://code.sololearn.com/WcpqtGnIWhxY/?ref=app

2nd Oct 2017, 11:52 PM
Gabriel AntƓnio
Gabriel AntƓnio - avatar
4 Answers
+ 5
window.onload = function() { var app = document.getElementById("app"); console.log(app); }; You need to wrap the code in an onload function so that it is run after the DOM is created. SL injects the JS and CSS tabs into the head of the html page before the body section and DOM is created.
3rd Oct 2017, 12:01 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
It's okay
3rd Oct 2017, 12:48 AM
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬ - avatar
+ 1
Thus is because you get a null object when you call for that element
3rd Oct 2017, 12:01 AM
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬ - avatar
+ 1
I wrapped the code inside the 'window.onload' function and worked fine, thank guys!
3rd Oct 2017, 12:06 AM
Gabriel AntƓnio
Gabriel AntƓnio - avatar