How to use confirm using sweet alert? How to use confirm using sweet alert? jquery jquery

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  swal({      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) {        swal({          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.

Demo: http://plnkr.co/edit/YTY7PDs5Uh1XGUo9ic1s?p=preview


You need To use then() function, like this

swal({    title: "Are you sure?",    text: "You will not be able to recover this imaginary file!",    type: "warning",    showCancelButton: true,    confirmButtonColor: '#DD6B55',    confirmButtonText: 'Yes, I am sure!',    cancelButtonText: "No, cancel it!" }).then(       function () { /*Your Code Here*/ },       function () { return false; });


document.querySelector('#from1').onsubmit = function(e){ swal({    title: "Are you sure?",    text: "You will not be able to recover this imaginary file!",    type: "warning",    showCancelButton: true,    confirmButtonColor: '#DD6B55',    confirmButtonText: 'Yes, I am sure!',    cancelButtonText: "No, cancel it!",    closeOnConfirm: false,    closeOnCancel: false }, function(isConfirm){   if (isConfirm){     swal("Shortlisted!", "Candidates are successfully shortlisted!", "success");    } else {      swal("Cancelled", "Your imaginary file is safe :)", "error");         e.preventDefault();    } });};