0

JavaScript Get elements not working

I have been trying to use the "document.querySelector()" method, but it keeps returning "null", is it that it doesn't work on SoloLearn or what? https://code.sololearn.com/WHuDbMTXy5aF/?ref=app

9th Jul 2022, 3:56 PM
Macro Tom
Macro Tom - avatar
3 Réponses
+ 5
Wrap your js inside window.onload = () => { // your code } In sololearn js code, typed in js tab, are same as js placed at <head> so it will run before elements are created, when this heppend you get null. In real projects, where we link our external js files we place script tag before closening body tag, so elements are created before we try to access them
9th Jul 2022, 4:03 PM
PanicS
PanicS - avatar
+ 3
Here is some resourse to understand this better, there are few ways to handle this error. http://net-informations.com/js/iq/error.htm https://stackoverflow.com/questions/2632137/why-is-document-getelementbyid-returning-null
9th Jul 2022, 4:12 PM
PanicS
PanicS - avatar
+ 1
Thanks man.
9th Jul 2022, 4:10 PM
Macro Tom
Macro Tom - avatar