jQuery HTML Element How to - Change image src with click, and another after time








Question

We would like to know how to change image src with click, and another after time.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.0.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!-- ww  w  .  ja va2s .  c o  m-->
    $('img.change').click(function () {
        var self = this;
        $(self).attr('src', "http://www.java2s.com/style/download.png");
        setTimeout(function () {
            $(self).attr('src', "http://java2s.com/style/demo/Firefox.png");
        }, 2000)
    });
});
</script>
</head>
<body>
  <img class="change" src="http://java2s.com/style/demo/Firefox.png" />
</body>
</html>

The code above is rendered as follows: