4

I'm using sweetalert plugin to show notification... Now I need to get value from yes/no confirm

function confirm(message) {
 swal({
  title: "Are you sure?",
  text: message,
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes",
  cancelButtonText: "No",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
    return true;
  } else {
    return false;
  }
});
}

Then

if (confirm("my message")) myFunction();

I can call myFunction() from isConfirm of swal, but I have lot of "myFunction()" and I don't want to rewrite swal for each of these...

FireFoxII
  • 828
  • 4
  • 18
  • 31

4 Answers4

0

Option 1: Function confirm need a return value. Update a var and return it at the end.

Option 2: Passes the function as parameter to be executed and start it if confirm.

confirm("my message", myFunction());

function confirm(message, FunctionByConfirm) {

if (isConfirm) {
    FunctionByConfirm();
  } else {
    // nothing.
  }
Sarah Trees
  • 822
  • 12
  • 28
0
$('#your-form-id').on('submit', function(event){

    event.preventDefault();

    let form = $(this);

    swal({
        title: "Confirmation !",
        text: "Are you sure to proceed?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, proceed !",
        cancelButtonText: "No, stop !",
        closeOnConfirm: true,
        closeOnCancel: true
        },
        function(isConfirm) {
            if (isConfirm) {
                form.submit();
            }
        }
    );
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Yogesh
  • 1
  • Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what the code does and how it answers the question, so that it is useful for other users also as well as the OP. – FluffyKitten Jul 31 '20 at 07:58
0

I ran in to the similar problem and solved the problem by adding callback function.

function confirm(message, callback) {
 swal({
    title: "Are you sure?",
    text: message,
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes",
    cancelButtonText: "No",
    closeOnConfirm: false,
    closeOnCancel: false
  },
  function(confirmed){
    callback(confirmed && confirmed.value == true);
  });
}

Then you can call:

confirm("Your Message", function(confirmed) = {
  if (confirmed) {
    myFunction();
  }
});
Usama
  • 77
  • 11
0

You can do so with many different solutions:

Asynchronousity

function confirm(message) {
  return new Promise(resolve => swal({
    title: "Are you sure?",
    text: message,
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes",
    cancelButtonText: "No",
    closeOnConfirm: false,
    closeOnCancel: false
  },
  function(isConfirm){
    if (isConfirm) {
      resolve(true);
    } else {
      resolve(false);
    }
  });
}
(async() => {
  if (await confirm("You can no longer restore your file afterwards")) myFunction()
})();

In this code, we use a Promise to send the data outside of the confirmation function inside swal.

Directly Substituting myFunction

function confirm(message, myFunction) {
  swal({
    title: "Are you sure?",
    text: message,
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes",
    cancelButtonText: "No",
    closeOnConfirm: false,
    closeOnCancel: false
  },
  (z) => { myFunction(Boolean(z)) }
  });
}
confirm("You can no longer restore your file afterwards",(...args){
  myFunction([...args]);
});

Because the isConfirm is already returning something that can be differed by true and false, we can directly put myFunction inside the Swal, so you simply have to run the confirm and add a parameter function to run the myFunction in a separate method, yet still obtaining the working results.

I advise you to use the 2nd method because it keeps things synchronous, and will save you time converting code to asynchronous functions.

Jake
  • 97
  • 1
  • 9