Showing posts with label Toggle image using javascript. Show all posts
Showing posts with label Toggle image using javascript. Show all posts

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