I started learning JavaScript week ago, and I am trying to code a slide show that also can change the title of the picture based on the picture. However, it replace the title with "Quenn4" which has the place 3 in my array…. or it says undefind.
Here is the code
and thanks for the help in advynce
// START OF Slideshow with Event Listener
(function(){
"use strict";
const myImages = ['sets/queen3.jpg', 'sets/queen4.jpg', 'sets/queen5.jpg', 'sets/queen6.jpg'];
let currentImage = 0;
const container = document.getElementById('content');
const nextBtn = document.getElementById('next');
const prevBtn = document.getElementById('previous');
function swap(){
const newSlide = document.createElement('img');
newSlide.src = myImages[currentImage];
newSlide.className = 'fadeinimg';
container.appendChild(newSlide);
if(container.children.length > 2){
container.removeChild(container.children[0]);
}
};
nextBtn.addEventListener('click', function(event){
event.preventDefault();
currentImage++;
if(currentImage > (myImages.length - 1)){currentImage = 0;}
swap();
txtSwitcher();
});
prevBtn.addEventListener('click', function(event){
event.preventDefault();
currentImage--;
if(currentImage < 0){currentImage = myImages.length - 1;}
swap();
});
function txtSwitcher(){
const queensNames = ["Queen1", "Queen2", "Queen3", "Queen4"];
const queen = document.getElementById('img-title');
let queenQue = 0;
for (let i = 0; i < (queensNames.length); i++) {
queenQue++;
queen.innerHTML = queensNames[i];
//console.log(queensNames[i]);
}
};
})();
// END OF : Slideshow Event Listener
Everything I could think of
2
Answers
Here are some points regarding different parts of your code:
This means that you want to have 2 images displayed at the same time. Is that correct? If you want just one image, change it to
container.children.length > 1
. Also, if you want the title to be updated every time you need to calltxtSwitcher()
insideprevBtn.addEventListener('click')
as well. Alternatively you can call it insideswap()
only.In this function you use
let queenQue
. This means that every time the function is called you resetqueenQue
to zero. After that in your for loop you go all the way to the last item inqueensNames
. What you need instead is declare the variables outside of your function. You can update the title using the current image index since it should correspond to the title index.Take a look at this demo which has your code with adjustments:
The problem is you are iterating over the current array with a for loop so the name gets replaced several times till reaching the last index of your queens array (e.g. 3). You need to Change the name of the current shown images by the current state of
currentImage
.Furthermore you can’t decide which queen you want to edit at the moment so you need a way to access the first and the second image because you are always displaying 2 images.
If you only want to display one image you need to change
if(container.children.length > 2){
toif(container.children.length >= 2){
to only display one queen at a time. You should never have the same id in one document twice so you should add the currentImage as index to the new image id you are creating with the setAttribute method of javascript.You have two arrays which are both the same length like following:
So the index 0 of the array queens is the image to the title in index 0 of the array queenTitles.
So your txtSwitcher() function should look like something like this:
Couldn’t test it out because your HTML was missing and some more information. But conceptional it should work like this.