How to change the source of an image using external js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How to change the source of an image using external js?

I am trying to change the image source of an image using external js like this:- var image = document.getelementbyId('image'); Image.src = 'newimage.src'; But it shows an error can't access the src value of null. But the same code works fine using internal js. Please help me!

26th Feb 2019, 12:31 PM
Ashutosh Agrawal
Ashutosh Agrawal - avatar
2 Answers
+ 5
You have to wrap your js codd inside the window.onload function to get rid of the null element message. window.onload = () => { //your code }
26th Feb 2019, 12:41 PM
Seniru
Seniru - avatar
+ 3
Its the usual problem of accessing to dom when is possible... See this code https://code.sololearn.com/WlA8Fx8bl6nI/?ref=app
26th Feb 2019, 1:31 PM
KrOW
KrOW - avatar