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

205
Visualizações
How to confirm a firm but set a function in-between?

I got a script that checks every form in my app and if there is one, that gets submitted, it will prevent the submission, toggles a loading screen and then submits the form.

<script>
  document.addEventListener('DOMContentLoaded', () => {

      const forms = document.querySelectorAll('form');
      forms.forEach((form) => {
        form.addEventListener('submit', (e) => {
          e.preventDefault();
          document.getElementById('loading').classList.toggle('hidden');
          form.submit();
        })
      });

  });

</script>

This works perfectly but for some forms (like a deletion button) I want to ask to user if this is really what he wants to do:

<form method="post" action="<?=base_url('clients/' . $meeting->cid);?>" onsubmit="return confirm('Are you sure?');">
  <input type="hidden" name="delmeeting" value="<?=$meeting->id;?>">
  <input type="submit" value="Delete" class="button">
</form>

This is in conflict with the first script. The alert gets shown correct but when I cancel it, the first script comes into place and submits.

How can I handle this?

What is important to me:

  • display the loading div every time for each form
  • having the possibility to have forms with and without a confirm window
  • keeping the code small
  • having individual texts for the confirm
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Got it:

const forms = document.querySelectorAll('form');
forms.forEach((form) => {
  form.addEventListener('submit', (e) => {
    if (form.onsubmit === null) {
      e.preventDefault();
      document.getElementById('loading').classList.toggle('hidden');
      form.submit();
    }
  })
});
``
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