jQuery <img> change the image source

Description

jQuery <img> change the image source

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>Change Image Source on Click in jQuery</title>
<style>
    .card{/*from   w  w  w  . j  ava 2  s  .co m*/
        margin: 30px;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $("img").click(function(){
        // Change src attribute of image
        $(this).attr("src", "image2.png");
    });
});
</script>
</head>
<body>
    <div class="card">
        <img src="image1.png" alt="image">
    </div>
  <p>Click on the image to change its source.</p>
</body>
</html>



PreviousNext

Related