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

212
Vistas
Triggering form's submit event doesn't execute event handler

If I have the following event handler bound to my form:

form.addEventListener('submit', function(evt) {
  evt.preventDefault();
  console.log('submitted');
  this.submit();
});

When the form is submitted by the client, the event handler is executed and the form is submitted without the event handler being executed again.

However, the same logic doesn't apply when attaching click handlers to anchor elements:

a.addEventListener('click', function(evt) {
  evt.preventDefault();
  console.log('clicked');
  this.click();
});

The event handler is executed twice and the link is never followed.

I have two questions:

  • Why does it appear to work differently for submit vs click?
  • Why does the anchor's click handler only get executed twice and not indefinitely, if the event handler is being executed each time it's called?
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Form submission and element click are different actions, they don't have to behave in the same way.

When using the submit method of the form, the standard says:

Submits the form, bypassing interactive constraint validation and without firing a submit event.

This can be seen also in the submission algorithm. Item 6 checks, whether the action comes from the submit method of the form, and skips the validation and event firing if the "submit()" flag is set.

element.click sets "click in progress flag", which is checked internally before creating a syntehtic click event which calls the hander function. This prevents a click on an element to lead to infinite recursive click event handler calls.

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