Skip to content Skip to sidebar Skip to footer

How To Set Src Of Image By A Function Call?

I want to set the src of an image to the return value of a function call. Here is what I am doing now: can and the script is:

Solution 1:

You can't set it like that.

You can change the value with javascript:

<img id="image" src="picB.png" alt="can't display picture" />

document.getElementById("image").setAttribute("src","picA.png");

Solution 2:

You can't do it that way. The src attribute doesn't interpret javascript.

You may do it like this:

<imgid="picA"><scripttype="text/javascript">functionget_picAPath(){
      return"picA.png";
   }
   document.onload = function(){
      document.getElementById('picA').src= get_picAPath();
   };
</script>

Post a Comment for "How To Set Src Of Image By A Function Call?"