Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda