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

154
Vistas
Does 'submit()' method impede 'submit' event listener to be executed?

I feel like I'm asking "what's 2+2 ?" but I'm facing an issue I don't understand... Here is the code, easy to understand:

<button type="button" onclick="send()">
<form id="form">
  <input name="name">
</form>

<script>
  const form = document.getElementById("form");
  function send() {
    form.submit();
  }
  form.addEventListener("submit", (e)=>{
    // This code never gets executed,
    // the default behavior still works.
    // (note that my console preserves logs,
    // so it should be visible even though the page is being refreshed.)
    console.log("preventing default behavior");
    e.preventDefault();
  });
</script>

Could someone help me on that one ? It should be so easy but for some reason the code doesn't want to be nice with me tonight.

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

0

This is the (somewhat unintuitive) expected behavior. See the docs for HTMLFormElement.submit():

This method is similar, but not identical to, activating a form's submit . When invoking this method directly, however:

  • No submit event is raised. In particular, the form's onsubmit event handler is not run.

  • Constraint validation is not triggered.

The HTMLFormElement.requestSubmit() method is identical to activating a form's submit and does not have these differences.

So, you can use form.requestSubmit instead of form.submit.

about 4 years ago · Juan Pablo Isaza Denunciar

0

  const form = document.getElementById("form");
  function send() {
    form.submit();
  }
  form.addEventListener("submit", (e)=>{
    // This code never gets executed,
    // the default behavior still works.
    // (note that my console preserves logs,
    // so it should be visible even though the page is being refreshed.)
    
    e.preventDefault();
    console.log("preventing default behavior");
    send();
  });
<form id="form">
  <input name="name">
  <button type="submit" >submit</button>
</form>

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