Update image src using JavaScript?

You are attempting to pass your image tag as smg1 but you don't declare that that is your image element.

try this:

SwitchPic('img1');

and then in your handler:

function SwitchPic(picID) {
  var pic = document.getElementById(picID);
  pic.src = "img.png";
}

In this scenario you pass the ID of the element you want to change then acquire the actual element inside your handler.


Might be just a cache problem

Instead of just using the filename, add a random part to it, like 'img.png?random', that way you'll force the browser to look for the image again on the server, but the ?random part will be discarded anyway and the file will still be found

Easiest way to do this is to add the date/time after the extension