Adding another image by setting src attribute - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

Adding another image by setting src attribute

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
function doImgSwap(el)//from ww w  .  j  av a2  s  .  co  m
{
  var img = document.createElement('img');
  img.setAttribute('src','http://www.java2s.com/style/download.png');
  el.appendChild(img);
}

      </script> 
   </head> 
   <body>   
      <p> 
         <a href="#" id="newImg" onclick="doImgSwap(this)">
            <img src="van.png">
         </a> 
      </p>   
   </body>
</html>

Related Tutorials