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

177
Views
¿Por qué EventListener recarga la página y onclick no?

Últimamente he tenido problemas con la recarga de una página durante el envío del formulario. Finalmente descubrí que la página se vuelve a cargar cuando envío el formulario con EventListener, pero no se vuelve a cargar con el envío del formulario al hacer clic. El siguiente código es cómo probé eso. ¿Puede alguien explicarme por qué?

 <html> <head> <title>Reload Page Test</title> <script> document.addEventListener("DOMContentLoaded", function () { document.querySelector("#asubmit").addEventListener("click", test); document.querySelector("#bsubmit").onclick = test; function test(){ const name = document.getElementById("name").value; const age = document.getElementById("num").value; document.getElementById( "result" ).innerHTML = `${name} is ${age}years old.`; return false; }; }); </script> </head> <body> <div> <form> <input type="text" id="name" /> <input type="number" id="num" /> <input type="submit" id="asubmit" value="Event Listener Submit"/> <input type="submit" id="bsubmit" value="On Click Submit"> </form>`enter code here` </div> <div id="result"></div> </body> </html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Devolver false desde una función onclick evitará el comportamiento predeterminado.

El valor de retorno de una devolución de llamada del detector de eventos no tiene sentido.

Use event.preventDefault() en su lugar.

 function test(event) { // ... event.preventDefault(); }
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!