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

208
Visualizações
Javascript - if statement not returning correct image from array (always behind)

I have an array of images:

const characterImages = ["images/jerry-senfeld.png", "images/george-costanza.png", "images/elaine-benes.png", "images/cosmo-kramer.png"]

I have another function which pulls some data from a basic API and updates a simple HTML page. An if statement looks at the innerHTML to determine which character is there and based on that, set the src attribute of an image. Everything is working as expected, however the image shown is always 1 index spot behind the quote. So for instance if they innerHTML of <h2> says "George" I can expect the jerry-seinfeld.png image to show. I have been going around in circles on this, any help would be appreciated. Code below.Thank you!

//Define our variables
const headerTag = document.querySelector("h2");
const quoteTag = document.querySelector(".quote");
let characterImage = document.querySelector(".characterImage");

const characterImages = [
  "images/jerry-senfeld.png",
  "images/george-costanza.png",
  "images/elaine-benes.png",
  "images/cosmo-kramer.png",
];
var x;
const getQuote = () => {
  fetch("https://seinfeld-quotes.herokuapp.com/quotes")
    .then((response) => response.json())
    .then((jsonData) => {
      quoteTag.innerHTML = jsonData.quotes[x].quote;
      headerTag.innerHTML = jsonData.quotes[x].author;
    });
};

// images function to go below:
const getImage = () => {
  if (headerTag.innerHTML == "Jerry") {
    characterImage.setAttribute("src", characterImages[0]);
  } else if (headerTag.innerHTML == "George") {
    characterImage.setAttribute("src", characterImages[1]);
  } else if (headerTag.innerHTML == "Elaine") {
    characterImage.setAttribute("src", characterImages[2]);
  } else if (headerTag.innerHTML == "Kramer") {
    characterImage.setAttribute("src", characterImages[3]);
  } else {
    characterImage.setAttribute("src", "images/placeholder-image.png");
  }
};

document.querySelector("button").addEventListener("click", function (event) {
  x = Math.floor(Math.random() * 422);
  getQuote();
  getImage();
});

HTML (relevant parts):

<div class="main-quote">
  <h2></h2>
  <img src="images/placeholder-image.png" class="characterImage" alt="character image">
  <p class="quote"></p>
  <button class="button2">Click!</button>
</div>
about 4 years ago · Juan Pablo Isaza
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