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

153
Views
Mi función se está ejecutando antes de que se haga clic en un botón

Estoy tratando de recorrer cada botón para verificar si se ha hecho clic en él, y si se ha hecho clic en un botón, entonces llame a la función test(). Sin embargo, se llama a la función y se registra "prueba" en la consola tres veces antes de que se presione cualquier botón. ¿Por qué se ejecuta la función antes de que el usuario presione un botón?

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Rock, Paper, Scissors</title> </head> <body> <h1>Rock, Paper, Scissors!</h1> <button id="ROCK">Rock!</button> <button id="PAPER">Paper!</button> <button id="SCISSORS">Scissors!</button> <script src="javascript.js"></script> </body> </html>
 function test() { console.log('test'); } function playGame() { //check whether user has clicked button const btn = document.querySelectorAll('button'); btn.forEach(button => { button.addEventListener("click", test())}); }
over 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
Virtual assistant