Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
i want to change image using for loop in js without using jQuery

i don't understand why this is not working? plz tell me what i missed in this code ** html **

<div id="img">
            <img class="images"src="images/boy1.jpg">
        </div>

** js **

var images = new Array();

images[0] = "/images/boy1.jpg";
images[1] = "/images/boy2.jpg";
images[2] = "/images/boy3.jpg";
images[3] = "/images/girl1.jpg";
images[4] = "/images/girl2.jpg";


function changeImg(){

    let img = document.querySelector(".images");   
    for(let i=0;i<images.length;i++){
       img.src = images[i]
    }
}

document.getElementById("btn").addEventListener("click",changeImg)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

try it:

  var images = new Array();

    images[0] = "/images/boy1.jpg";
    images[1] = "/images/boy2.jpg";
    images[2] = "/images/boy3.jpg";
    images[3] = "/images/girl1.jpg";
    images[4] = "/images/girl2.jpg";


    function changeImg() {

    
      const imgBox = document.getElementById("img");
      for (let i = 0; i < images.length; i++) {
        const img = document.createElement("img");
        img.src = images[i]
        imgBox.appendChild(img)

      }
    }

    document.getElementById("btn").addEventListener("click", changeImg)
<div id="img"></div>
<button id="btn">Start</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your loop always takes the last image and sets it to be the source of the img tag, what you need to be doing is changing the index and then only set the indexed image to be the source:

const image = document.querySelector(".images");
let currentIndex = 0;
let images = new Array();

images[0] = "/images/boy1.jpg";
images[1] = "/images/boy2.jpg";
images[2] = "/images/boy3.jpg";
images[3] = "/images/girl1.jpg";
images[4] = "/images/girl2.jpg";

const length = images.length;

function changeImg(){
    // increment currentIndex in a circular way; after 4 comes 0 again
    currentIndex = ++currentIndex % length;
    image.src = images[currentIndex];
}

document.getElementById("btn").addEventListener("click", changeImg);
about 4 years ago · Juan Pablo Isaza Relatório

0

Your for loop changes img.src five times immediately and so the final result is images[4], you don't see the other images.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda