Tuesday, 11 February 2014

Toggle image on click using javascript

Put any of  your two images in folder images/
<!doctype>
<html>
<head>
<script>
   function changeImage(img){
      var imgfile = img.src.split('/').pop();
 if(imgfile == "happy.png")
 {
    img.src ="images/sad.png";
 }
 else{
   img.src = "images/happy.png";
 }
   }
</script>
</head>
<body>
  <img src='images/happy.png' onclick='changeImage(this)'>
</body>

Online Demo

No comments:

Post a Comment

Thank your for your comment..your submitted coment will be live after admin approval