I am trying to select HTML elements using document.getElementById method but it keep showing null reference. how to fix it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I am trying to select HTML elements using document.getElementById method but it keep showing null reference. how to fix it?

11th Apr 2019, 12:56 PM
Vivek
Vivek - avatar
4 Answers
+ 11
Try to wrap ur js code in : window.onload =function( ) { / / Write ur code here.. } Also make sure that u have an html element with that id. If ur code still doesn't get fixed, post it here so that we can help.
11th Apr 2019, 1:12 PM
Arushi Singhania
Arushi Singhania - avatar
+ 9
You can use script tag in both head and body.. But if u are putting it in head, u need that function so that ur javascript will run only after html loads so that u can grab html elements.....
11th Apr 2019, 1:59 PM
Arushi Singhania
Arushi Singhania - avatar
0
Thanks It worked!!! script tag must in the head tag behind the scenes, right?
11th Apr 2019, 1:39 PM
Vivek
Vivek - avatar
0
Thanks!
11th Apr 2019, 2:36 PM
Vivek
Vivek - avatar