SweetAlert2 - Bind another event to cancel button?

sweet alert 2 cancel button event
sweetalert2 multiple buttons
sweetalert change width
sweetalert change background color
sweetalert loading
sweetalert2 cdn
sweetalert2 ajax
sweetalert2 form

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?

  1. You could create a custom html file and have a cancel button in that which will fire off you own cancel handler.

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
        });
    });
  1. 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
   }
});

sweetalert - SweetAlert2, 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. I have a Yes and No button in my SweetAlert2. When I click on No it does a post to a method but I just want it to close the SweetAlert. Here is the code I have written: $('.js-update-details-

Just add your custom function to catch the rejection, for example:

swal({
   title: 'Some title',
   text: 'some text for the popup',
   type: 'warning',
   showCancelButton: true,
   cancelButtonText: 'Some text for cancel button'
}).then(function(){
   // function when confirm button clicked
}, function(dismiss){
   if(dismiss == 'cancel'){
       // function when cancel button is clicked
   }
});

You can even add more function to catch another dismiss event, just read SweetAlert2 Docs for more info about dismiss event.

SweetAlert2 - Bind another event to cancel button? - button - html, 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. Even if I had to assign it numbers - 0, 1, and undefined/false for cancel, that would be sufficient. Given that even windows has yes/no/cancel dialogs, I think it's strange to not at least support 3 buttons, but even stranger is not supporting N buttons (and leaving it up to the implementer to deal with issues like how many go in a row, etc).

with a little customization to @Raditya Adi Baskara answer,

swal({
        title: "$titleWarnignBox",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#36c6d3',
        cancelButtonColor: '#d33',
        confirmButtonText: '$confrimBtn',
        cancelButtonText: '$cancelBtn'
    }).then(function(result){
        if(result.value){
            console.log('good');
        }else if(result.dismiss == 'cancel'){
           console.log('cancel');
        }

    });

How to handle cancel button action · Issue #656 · t4t5/sweetalert , We have a callback parameter for the confirm button, but how can we use a Events · Community forum · GitHub Education You can find an example of how to do this in the documentation: http://t4t5.github.io/sweetalert/ The actual issue is that in sweetalert v1.x the cancel handler is checking for the  If set to true, a "Cancel"-button will be shown, which the user can click on to dismiss the modal. confirmButtonText 'OK' Use this to change the text on the "Confirm"-button. cancelButtonText 'Cancel' Use this to change the text on the "Cancel"-button. confirmButtonColor: null: Use this to change the background color of the "Confirm"-button.

In sweetalert 2

            swal.fire({
                title: "Notice",
                text: "Are you sure ?",
                showCancelButton: true,
                type: 'error',
                cancelButtonColor: '#d33',
            })
                .then((res) => {
                    if(res.value){
                        console.log('confirmed');
                    }else if(res.dismiss == 'cancel'){
                        console.log('cancel');
                    }
                    else if(res.dismiss == 'esc'){
                        console.log('cancle-esc**strong text**');
                    }
                });

Callback is not called when pressing the cancel button · Issue #573 , The confirm button always calls the callback with an argument of true, but the cancel callback never calls the Events · Community forum · GitHub Education This method fails if you change the context of the callback using bind such as swal({. It seems like this repo is inactive, I made sweetalert2. The Problem The issue is from bootstrap modal accessibility control code which can be found here Modal Accessibility Tab Control, it enforces focus on the Bootstrap modal whenever it loses focus to another element( in this case SweetAlert Modal) in the page. WorkAround It's as simple as removing the focus event.

SweetAlert, So what if you need more than just a cancel and a confirm button? Don't worry, SweetAlert's got you covered! By specifying an object for buttons , you can set  If set to true, a "Cancel"-button will be shown, which the user can click on to dismiss the modal. confirmButtonText 'OK' Use this to change the text on the "Confirm"-button. cancelButtonText 'Cancel' Use this to change the text on the "Cancel"-button. confirmButtonColor undefined: Use this to change the background color of the "Confirm"-button.

a beautiful, responsive, customizable and accessible , A modal with a title, an error icon, a text, and a footer A confirm dialog, with a function attached to the "Confirm"-button. cancelButtonText: 'No, cancel! ⏫ Always upgrade SweetAlert2 when you upgrade ngx-sweetalert2. The latter is statically linked with SweetAlert2's type definitions. Angular and SweetAlert2 versions compatibility table (click to show)

SweetAlert: Killing it with Promises > JavaScript for PHP Geeks , For our last trick, Google for a library called [SweetAlert2][sweetalert2]. The Event Argument & stopPropagation Fixing "this" with bind() And you can customize it in a lot of ways, like having a "Yes" and "Cancel" button. We're going to use SweetAlert so that when we click the delete icon, an alert opens so the user can  when i click ok button of sweet alert.. send to another page function/event triggered on click of OK button genereated a "Cancel" button in your modal, you'll

Comments