Change the value of the source attribute (src) of an <img> element, if the user clicks on the image: - Javascript Language Basics

Javascript examples for Language Basics:HTML

Description

Change the value of the source attribute (src) of an <img> element, if the user clicks on the image:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImage" onclick="changeImage()" src="http://java2s.com/resources/a.png" width="100" height="180">

<script>
function changeImage() {/* w w  w  .  j a  v a 2 s.c  o m*/
    var image = document.getElementById("myImage");
    if (image.src.match("bulbon")) {
        image.src = "pic_bulboff.gif";
    } else {
        image.src = "pic_bulbon.gif";
    }
}
</script>

</body>
</html>

Related Tutorials