Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
Agregar Event Listener a una imagen creada en js

Estoy tratando de crear un montón de imágenes dentro de js y agregar detectores de eventos a cada una de ellas para que activen la función cuando se haga clic.

Utilizo esta función para crear imágenes.

 function image(fileName) { const img = new Image(100, 100); img.src = `images/${fileName}`; return img; }

Cuando intento agregar un detector de eventos con image.addEventListener(...) se compila pero no funciona al dibujar las imágenes más adelante.

También intenté agregar una identificación a la imagen y usar getElementById() para agregar el oyente, no se compila con el oyente de eventos leyendo nulo.

¿Alguna sugerencia?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Los eventListeners solo funcionan cuando el elemento se agrega al DOM, que no es en su caso.

Si agrega su imagen al DOM, puede agregar un evenListener que funcione.

 function image(fileName) { const img = new Image(100, 100); img.src = `images/${fileName}`; // Or any other node document.body.appendChild(img); return img; } // Now you can bind eventListeners to the returned img element
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!