Sweetalert: how to pass an argument to a callback

I am using javascript sweetalert alert library

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 to pass id function from foo() to callback function in swal?

+5
source share
2 answers
 function(id){ alert(MyId); swal("Deleted!", "Your imaginary file has been deleted.", "success"); }); 

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

This will work - no additional variable is needed:

 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's jsfiddle: http://jsfiddle.net/60bLyy2k/

+6
source

just put your parameter in a local variable, they are available in the internal 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 is the fiddle https://jsfiddle.net/

+2
source

All Articles