Skip to content Skip to sidebar Skip to footer

Javascript Change Picture On The Go

Updating the Question: @Mouser, thank you for this answer. though I'm sure the code is fine, it actually doesn't do anything. I believe it is because I run the script right after t

Solution 1:

This code will not work. You're requesting the src property on a string. That isn't an image element. document.getElementById("testname1") maybe. So set the src of that element to currentname.

urlArray = [[0, "si"], [1, "wi"], [2, "fi"]];

for (var i=0; i<urlArray.length; i++) {
    var imgname = "listimg";
    if (urlArray[i][1]==="fi"){
      var currentname = imgname.concat(i);
      if(document.getElementById(currentname) ) //check if element exists
      {
         document.getElementById(currentname).src= "write.png";
         //for debugging; delete from production code.console.log(document.getElementById(currentname).src); //should write "write.png" to console.
      }
    }
}
<imgsrc=""id="listimg2" />

Post a Comment for "Javascript Change Picture On The Go"