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

215
Views
Javascript: ¿Cómo generar múltiples funciones en un bucle for?

En una página html, necesito agregar detectores de eventos para varios botones. Dado que la función de escucha para estos botones es muy similar, quiero usar un ciclo for para terminar de esta manera:

 var button_list=[button1,button2]; // Let's say 'button1' and 'button2' are button elements. var i=0; for(var button of button_list) { i+=1; button.addEventListener('click', function() { console.log(i); // I want button1 to print '1' and button2 to print '2' }); }

Quiero que button1 imprima 1 y button2 para imprimir 2 . Pero el código anterior no funciona ya que la variable i=2 eventualmente, por lo que todos los botones en los que se hizo clic imprimirán 2 . ¿Cómo puedo resolver el problema?

about 4 years ago · Juan Pablo Isaza
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!