Friday, 4 June 2021

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folder/directory.

<script>  

  $('.img1').on('click',  function() {

    $('#image').prop('src', "2.jpeg");

});

 $('.img2').on('click',  function() {

    $('#image').prop('src', "1.jpg");

});

$('.img3').on('click',  function() {

    $('#image').prop('src', "3.jpg");

});

</script>

Complete Example -

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

</head>

<body>

 <center>

  <img id="image" src="" style="margin-left:25%;margin-right:25%" height=80 width=80>

        <a  class="img1" href="#" title="image1">Image 1</a>

<a  class="img2" href="#" title="image2">Image 2</a>

<a  class="img3" href="#" title="image1">Image 1</a>

<script>  

  $('.img1').on('click',  function() {

    $('#image').prop('src', "2.jpeg");

});

 $('.img2').on('click',  function() {

    $('#image').prop('src', "1.jpg");

});

$('.img3').on('click',  function() {

    $('#image').prop('src', "3.jpg");

});

</script>

</center>

</body>

</html>

2 comments:

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...