+ 1

document.getElementById error.

Hi guys. I have little problem with thing like this document.getElementById("xxx").src="some some link.jpg" ; I want to change html tag (with id xxx) source from one to another but what I get is bunch of errors Type Error : document.getElementById(...) is null

16th Jan 2018, 9:28 PM
Karol Michalik
Karol Michalik - avatar
3 Respuestas
+ 5
always remember to interact with the page only after it has been loaded, what you probably did is requesting the element when it hasn't been loaded yet, to prevent this use window.onload window.onload = function() { document.getElementById("xxx").src = "somePic.jpg" }
16th Jan 2018, 9:37 PM
wout werkman
wout werkman - avatar
+ 1
Haha yes this solved the problem. Thank you! I shall remember this. There is still so much to learn...
16th Jan 2018, 9:40 PM
Karol Michalik
Karol Michalik - avatar
- 1
This error TypeError: document.getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag. http://net-informations.com/js/iq/error.htm
25th Feb 2019, 10:11 AM
jacobhalls