简体   繁体   中英

how to change an image source more than once using onclick on html and javascript

I am trying to have an image, once clicked move on to image 2, then once image 2 is clicked it moves on to image 3, then image 4 and so on, but I am struggling to find a way to have more than 2 images? So far I have tried various different ways such as repeating the code I already have, using multiple if statements and switch statement but I just cannot seem to be able to use more than 2 images. I am only a beginner coder so it is difficult to see where I am going wrong. the expected outcome would be just to have a number of images appearing after each one is clicked.

So far the code that I have that's working is:

HTML:

<!DOCTYPE html>

<html>



  <head>



    <meta charset="utf-8"/>

  <title>Social Media</title>

  <link rel="stylesheet" type="text/css" href="css/styles.css">

  <script type="text/javascript">



    var mysrc = "image1.jpg";

        function changeImage() {

            if (mysrc == "image1.jpg") {

                document.images["pic"].src = "image1.jpg";

                document.images["pic"].alt = "image1";

                mysrc = "image.jpg";

                }



            else {

                document.images["pic"].src = "image.jpg";

                document.images["pic"].alt = "image";

                mysrc = "image1.jpg";

            }         



        }

    </script>

</head>

<body>

    <img src="image.jpg"

        alt="image" id="pic" class="portrait"

        onclick="changeImage()"

        width="1000px" height="500px"

        style="cursor:pointer">







</body>

</html>

and i have been able to get the same results by doing:

function change() {
var image = document.getElementById('image');
image.src = "image1.jpg"
}


</script>

</head>

<body>

<img src="image.jpg" alt="text" id="image" onclick="change();">

but i just cant seem to get more than 2 images? as mentioned I am just a beginner so I'm really not sure if it's just me making stupid mistakes, any advice would be really helpful

You should be to do this using an array and a simple variable counter.

var imgCount = 0;
var images = ["img1.png", "img2.png", "img3.png", "img4.png"...];

imgCount will get added to every time <img> is clicked.

<script>
var imgCount = -1;
var images = ["img1.png", "img2.png", "img3.png", "img4.png"...];
function change() {
  imgCount++;
  var image = document.getElementById('image');
  image.src = images[imgCount];
}
</script>
<img src="image.jpg" alt="text" id="image" onclick="change()">

The src is now called from the array. imgCount serves as the counter of the amount of times the image has been clicked and is used to find the correct src with the array since it also serves as an index.

If you don't know much about arrays read MDN .

I'm sure that you don't have an endless amount of pictures, so you can also use this:

<script>
var imgCount = -1;
var images = ["img1.png", "img2.png", "img3.png", "img4.png"];
function change() {
  if (imgCount !== images.length - 1)
    imgCount++;
  else 
    imgCount = 0;
  var image = document.getElementById('image');
  image.src = images[imgCount];
}
</script>
<img src="image.jpg" alt="text" id="image" onclick="change()">

Now, if <img> is clicked and it is on the last image, it will go back to the beginning. The if statement sees whether or not the counter equals the amount of images in the array. If it hasn't it keeps adding. If it hasn't, it sets the counter back to 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM