4th Feb 2018, 6:23 PM
Valkyrion
Valkyrion - avatar
5 Answers
+ 3
That's because his/her code is running once all the elements of the page have been loaded. In your case the script was running before the elements were loaded https://www.w3schools.com/jsref/event_onload.asp
4th Feb 2018, 7:01 PM
Mickel
Mickel - avatar
+ 1
It is because in the first line you are obtaining an element that does not exist (there is no element with the id 'Block') and then you are showing that element in the console (since there is no element with the id 'Block', your variable is equal to null )
4th Feb 2018, 6:40 PM
Mickel
Mickel - avatar
+ 1
window.onload = function() { var block = document.getElementById("block"); console.log(block); // <-- try alert(block); instead here. }; Note that in this case console.log() won't show anything. Try using alert() instead. alert() will show the string definition of the HTML object in this case.
4th Feb 2018, 6:51 PM
ChaoticDawg
ChaoticDawg - avatar
0
But the id of my img is block what should i do ?
4th Feb 2018, 6:42 PM
Valkyrion
Valkyrion - avatar
0
Why do my code doesn't works but your code works ?
4th Feb 2018, 6:53 PM
Valkyrion
Valkyrion - avatar