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

177
Vistas
I'm trying to add a click event to my image without success in Javascript

I've got an array of images which display on button click in random order. I've confirmed that all images are clickable and that they display the correct image source in the console. What I am unable to do is create a on click event that alerts a message when an image is clicked. When I click image "A1", alert "No" is displayed, so I'm guessing that the problem may be that the image string and the actual image aren't linked together?

const images$front = [
      { name: "A1", img: "A1.png" }, 
      { name: "B1", img: "B1.png", },
      { name: "A2", img: "A2.png", },
      { name: "B2", img: "B2.png", }, 
      { name: "A3", img: "A3.png", },
      { name: "B3", img: "B3.png", }, 
      { name: "A4", img: "A4.png", },
      { name: "B4", img: "B4.png", },  
      ];
//Rear image
const images$rear=[
      { name: "z",  img: "z.png" , },
];

function shuffle(array) {
  let currentIndex = array.length,
    randomIndex;

  while (currentIndex != 0) {

    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]
    ];
  }
}

shuffle(images$front)

function DisplayImage(i) {
  let CardImage = document.createElement('img');
  CardImage.src = `Images/${images$front[i].img}`
  CardImage.alt = CardImage.src;
  document.querySelector("#box").appendChild(CardImage);
  CardImage.addEventListener("click", function(e) {
    if (CardImage === "A1") {
      alert("Yes")
    } else {
      alert("No")
    }
    console.log(e.target.src)
  })
}

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your condition is trying to equal between the object to string, CardImage is HTMLImageElement, that always will be false , you can add another property "name" and then make the condition.

function DisplayImage(i) {
  let CardImage = document.createElement('img');
  CardImage.src = `Images/${images$front[i].img}`
  CardImage.alt = CardImage.src;
  CardImage.name = images$front[i].name; //here is the change.
  document.querySelector("#box").appendChild(CardImage);
  CardImage.addEventListener("click", function(e) {
  if (CardImage.name === "A1") { //change here too.
     alert("Yes")
   } else {
    alert("No")
   }
   console.log(e.target.src)
})
}
about 4 years ago · Juan Pablo Isaza Denunciar
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