jQuery attr() change the image source

Description

jQuery attr() 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{//  w ww  .  j  av a 2 s .c o  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