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?
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.
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. }
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!")
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/
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With