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
3 Antworten
+ 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
+ 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
+ 1
Thanks man.