I am using the latest version of the awesome SweetAlert2 jquery plugin.
I have a simple SweetAlert with 2 buttons. 1 button is the confirm button, the other is the cancel button. I am using the html option to add a text input to the alert. When the user press the confirm button an AJAX call is executed and the alert is closed.
Now I want to use the cancel button to execute some other code instead of the default action which is closing the alert. (The user can close the alert using the showCloseButton: true).
So in short: How to remove the closing handler and add a own custom handler to the cancel button of swal?
for example
<html> <!--custom.html-->
<button id="confirmBtn">confirm<button>
<button id="cancelBtn">cancel<button>
<html>
$.get("custom.html", function (data) {
swal({
html: data,
showCloseButton: false,
showCancelButton: false,
showConfirmButton: false
});
$("#cancelBtn").click(function () {
//handle your cancel button being clicked
$.when($.ajax({....})).then(function() {
// when all AJAX requests are complete
});
});
$("#confirmBtn").click(function () {
//handle your confirm button being clicked
});
});
You could just recall the popup on cancel. Add this to your swal function.
function (dismiss) {
if (dismiss === 'cancel') {
swal({..});
}
}
So in full
swal({
title: 'Swal Title',
text: 'Your swal text',
type: 'warning',
showCancelButton: true,
cancelButtonText: 'cancel'
}).then(function(){
//Confirmed
}, function(dismiss){
if(dismiss == 'cancel'){
//swal({..}); //un-comment this line to add another sweet alert popup on cancel
}
});