Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sweetalert: how pass argument to callback

I am using javascript alert library sweetalert

My code is:

function foo(id) {
    swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
    },
    function(){
      swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
}

How can I pass id from foo() function to callback function in swal?

like image 389
Metalik Avatar asked Aug 26 '15 05:08

Metalik


People also ask

What does Swal () from SweetAlert library do?

swal("Click on either the button or outside the modal.") This comes in handy if you want to warn the user before they perform a dangerous action. We can make our alert even better by setting some more options: icon can be set to the predefined "warning" to show a nice warning icon.

How do I return a value from SweetAlert?

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. }

How do you call SweetAlert?

Once the library is set up, creating a SweetAlert message is very easy. All you have to do is call the swal() function. swal("Here's a message!", " Have a nice day!")


2 Answers

function(id){
  alert(MyId);
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
});

This will not work, because in this case id is the option isConfirm for your confirmation dialog - see SweetAlert Documentation.

This will work - no need for an additional variable:

function foo(id) {
  swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    closeOnConfirm: false
  },
  function(isConfirm){
    alert(isConfirm);
    alert(id);
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  }); 
} 
foo(10);

here the jsfiddle: http://jsfiddle.net/60bLyy2k/

like image 113
Mario Murrent Avatar answered Oct 22 '22 15:10

Mario Murrent


just put your parameter in local variable they are accessible in inner function or in clousers

function foo(id) {
    var MyId = id;
    swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
    },
    function(){
        alert(MyId);
      swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
}

foo(10);

here the fiddle https://jsfiddle.net/

like image 27
Shailendra Sharma Avatar answered Oct 22 '22 15:10

Shailendra Sharma