+ 11

I sometimes have some problems with selecting elements from DOM. For example in this code:

var player = document.getElementById("player"); It shows error that cannot find property of undefined or null. Why?

26th Jan 2017, 4:05 PM
Aydin
7 Answers
+ 2
If the script tag with the code is above the element with Id "player" in the document, then it will try to get the player element before the element has loaded in. This happens because the script will be executed before everything below it in the document gets the chance to load You can fix this by moving the script below the element with Id "player" or by making a window.onload function so the code waits until the whole page has fully loaded before doing anything.
26th Jan 2017, 4:28 PM
Mini
+ 10
Thanks everybody! It now works!!!
26th Jan 2017, 5:02 PM
Aydin
+ 8
i am sure
26th Jan 2017, 4:38 PM
Aydin
+ 3
Um, what do you want to do with the variable "player"?
26th Jan 2017, 4:14 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
hey guys , i still have that problem , i have introduced an id in my script positioned above the script link code but the changes arent still visible ,please help.Am using opera mini and note pad.
14th Feb 2017, 7:32 AM
Fredrick Maina Gikuru
Fredrick Maina Gikuru - avatar
+ 1
use php Storm app. it shows u the errors very good
8th Feb 2017, 11:04 AM
hossy
hossy - avatar
+ 1
:)
21st Mar 2017, 2:51 PM
Ekhdemni tn
Ekhdemni tn - avatar