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

126
Views
Make modal show only when form is submitted successfully

I have a Boostrap 5 form with validations and when I add the data-bs-target attribute to send button the modal is triggered even if the form has invalid entires. I want to make the model triggered only when the form is successfully validated. I tried it with javascript but my code didn't work Below is my approach:


<div class="form-button mt-3">

 <button id="submit" type="submit" class="site-btn " data-bs-toggle="modal"  data-bs-target="">Send</button>

 </div>


<script type="text/javascript">
    (function() {
      'use strict'
      const forms = document.querySelectorAll('.requires-validation')
      Array.from(forms)
        .forEach(function(form) {
          form.addEventListener('submit', function(event) {
            if (!form.checkValidity()) {
              event.preventDefault()
              event.stopPropagation()
            }

            form.classList.add('was-validated'),
            document.getElementById('submit').dataset.target ='#confrimationModal';
         

          }, false)
        })
    })()
  </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You may programmatically open modal

const modal = new bootstrap.Modal(document.getElementById('confrimationModal'));
modal.show();

instead of

document.getElementById('submit').dataset.target ='#confrimationModal';

ref https://getbootstrap.com/docs/5.0/components/modal/#via-javascript

about 4 years ago · Juan Pablo Isaza Report

0

  1. Do not call anything submit
  2. Delegate
  3. manually trigger the modal
  4. Spelling (confirmationModal)
  5. dataset.bsTarget to change data-bs-target
(function() {
  'use strict'
  document.addEventListener('submit', function(event) {
    const tgt = event.target;
    if (tgt.matches(".requires-validation") && !tgt.checkValidity()) {
      event.preventDefault()
      event.stopPropagation()
    } else {
      tgt.classList.add('was-validated');
      document.getElementById('submitButton').dataset.bsTarget = '#confirmationModal';
    }
  })
})()
<div class="form-button mt-3">

  <button id="submitButton" type="submit" class="site-btn" data-bs-toggle="modal" data-bs-target="">Send</button>

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