Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda