Uncaught TypeError: Cannot set property 'src' of null

I am using the following line of code to change the src of an img with a div.
document.getElementById(idToTarget).src = SRC;
although the console is saying that document.getElementById(idToTarget) is equal to null, even though I set the img’s id to image and idTotarget also equals image so why is this happening?

1 Like

If your img element looks like this,

<img id="idToTarget" src="">

and SRC is set to a valid resource URI, then

document.getElementById('idToTarget').src = SRC

is how that line would be written.