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

198
Views
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 answers
Answer question

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 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!