change the src of an image to a variable in Javascript - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

change the src of an image to a variable in Javascript

Demo Code

ResultView the demo in separate window

<!doctype html>
<html>
   <head> 
      <script>
function setImage(){/*from w w w.jav  a2s  .  co m*/
    var thumbPrefix = "http://your server.com/";
    var thumbSuffix = "/default.jpg";
    var vidCode = ['f1','a6','P4'];
    var thumb1Url = thumbPrefix + vidCode[0] + thumbSuffix;
    document.getElementById('pie').src=thumb1Url;
}

      </script> 
   </head> 
   <body onload="setImage"> 
      <img src="" id="pie">  
   </body>
</html>

Related Tutorials