+ 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
3 Answers
+ 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"
}
+ 1
Haha yes this solved the problem. Thank you!
I shall remember this. There is still so much to learn...
- 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