How to use confirm using sweet alert?

You will need to prevent default form behaviour on submit. After that you will need to submit form programmatically in case of Ok button is selected.

Here is how it could look like:

document.querySelector('#from1').addEventListener('submit', function(e) {
  var form = this;

  e.preventDefault(); // <--- prevent form from submitting

      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      icon: "warning",
      buttons: [
        'No, cancel it!',
        'Yes, I am sure!'
      dangerMode: true,
    }).then(function(isConfirm) {
      if (isConfirm) {
          title: 'Shortlisted!',
          text: 'Candidates are successfully shortlisted!',
          icon: 'success'
        }).then(function() {
          form.submit(); // <--- submit form programmatically
      } else {
        swal("Cancelled", "Your imaginary file is safe :)", "error");

UPD. Example above uses sweetalert v2.x promise API.


Leave a Comment