Why does document.getElementById only work if the js is inside the script-tag and not external? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why does document.getElementById only work if the js is inside the script-tag and not external?

10th Jun 2017, 10:04 AM
Elias
Elias - avatar
3 Answers
+ 4
if it is external, it needs to be in a window.onload function, any script needing 'document' before doesnt load until the DOM, or page elements, load
11th Jun 2017, 6:12 AM
Russel Reeder
Russel Reeder - avatar
+ 1
why would it work when its a part of JavaScript
10th Jun 2017, 12:31 PM
_Retr0/-
_Retr0/- - avatar
+ 1
any JavaScript outside the script function will simply not work
10th Jun 2017, 12:31 PM
_Retr0/-
_Retr0/- - avatar