0

How do I complete the execute function, such that when the Next Image button is clicked, the next image in the images array is shown, and when I get to the end of the array, it should start from the beginning of the array.. so essentially, the Next Image button acts like a way to cycle through the images in the images array. NB: the image at the first index of the array is already shown. So when I click the button, the image at the second index should be shown.. and so on.

<!DOCTYPE html>
<html>

  <head>

  </head>

  <body>
    <button onclick='execute()'>Next Image</button>
    <div>
      <img src='https://bocageanimalhospital.com/wp-content/uploads/2020/09/iconfinder_1F431-cat-face_4888130.png' />
    </div>
  </body>
  <script>
    const images = [
  'https://bocageanimalhospital.com/wp-content/uploads/2020/09/iconfinder_1F431-cat-face_4888130.png',
  'https://catdoctorofmonroe.com/wp-content/uploads/2020/09/iconfinder_1F408-cat-B_4888072.png',
  'https://aux.iconspalace.com/uploads/cat-clean-icon-256.png'
    ]

    function execute() {
  
    }

  </script>

</html>
1
  • What have you tried so far? Commented Jul 8, 2021 at 9:05

2 Answers 2

1

Keep an index variable outside of the execute function and increment it with every click of the Next button. If the index value exceeds the length of the images array then reset this by 0.

const images = [
  'https://bocageanimalhospital.com/wp-content/uploads/2020/09/iconfinder_1F431-cat-face_4888130.png',
  'https://catdoctorofmonroe.com/wp-content/uploads/2020/09/iconfinder_1F408-cat-B_4888072.png',
  'https://aux.iconspalace.com/uploads/cat-clean-icon-256.png'
];

const nextBtn = document.getElementById('next-btn');
const image = document.getElementById('image');
let imageIndex = 0;
image.setAttribute('src', images[imageIndex]);

const execute = (event) => {
  imageIndex++;

  if (imageIndex >= images.length) {
    imageIndex = 0;
  }

  image.setAttribute('src', images[imageIndex]);
}

nextBtn.addEventListener('click', execute);
<img src="https://bocageanimalhospital.com/wp-content/uploads/2020/09/iconfinder_1F431-cat-face_4888130.png" id="image" />
<button id="next-btn">Next</button>

Sign up to request clarification or add additional context in comments.

Comments

0

You can have a simple index tracker for the array while looping and reset if it reaches the end of the images array.

<!DOCTYPE html>
<html>

<head>

</head>

<body>
  <button onclick='execute()'>Next Image</button>
  <div>
    <img id="image-holder" src='https://bocageanimalhospital.com/wp-content/uploads/2020/09/iconfinder_1F431-cat-face_4888130.png' />
  </div>
</body>
<script>
  const images = [
    'https://bocageanimalhospital.com/wp-content/uploads/2020/09/iconfinder_1F431-cat-face_4888130.png',
    'https://catdoctorofmonroe.com/wp-content/uploads/2020/09/iconfinder_1F408-cat-B_4888072.png',
    'https://aux.iconspalace.com/uploads/cat-clean-icon-256.png'
  ]
  var cIndex = 0

  function execute() {
    cIndex = cIndex + 1
    if (cIndex > images.length - 1) {
      cIndex = 0
    }
    document.getElementById("image-holder").src = images[cIndex]
  }
</script>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.