Sweetalert: Prevent auto close sweet alert

Created on 10 May 2015  路  7Comments  路  Source: t4t5/sweetalert

Trying to do simple stuff. I do not want to close popup until user click on confirm button (I turned off this option but popup is still closing). Here is my code:

sweetAlert({
    title: "Hello",
    text: "<button type='button' class='btn btn-fb fb-share'>Share on Facebook</button>",
    type: null,
    confirmButtonText: "Close",
    html: true,
    closeOnConfirm: false, //It does close the popup when I click on close button
    closeOnCancel: false,
    allowOutsideClick: false
});

$(document).on( "click",".fb-share",  function(e){
    //here sweet alert closes when I press this button. 
    openFbPopup(); 
});

How do I prevent closing sweet alert?

Most helpful comment

I know this is late, but might help somebody in the future

@moazam1 The option has been changed to

swal({
  closeOnClickOutside: false,
});

https://sweetalert.js.org/docs/#closeonclickoutside

All 7 comments

I also seem to be having issues with this. Even when I set allowOutsideClick to false, it is still dismissible.

@moazam1 For your specific case, you need to do a callback as an additional parameter after the options object:

sweetAlert({
    title: "Hello",
    text: "<button type='button' class='btn btn-fb fb-share'>Share on Facebook</button>",
    type: null,
    confirmButtonText: "Close",
    html: true,
    closeOnConfirm: false, //It does close the popup when I click on close button
    closeOnCancel: false,
    allowOutsideClick: false
}, function () {});

$(document).on( "click",".fb-share",  function(e){
    //here sweet alert closes when I press this button. 
    openFbPopup(); 
});

It doesn't need to do anything, but using a function like:

function (isConfirm) {
   // ... Rest of code
}

Allows you to respond manually and do something else.

Makes sense, as if you don't have a handler, how are you going to close the alert otherwise.

If you are using an anchor tag, just remove the href="", because it redirects you to the same page. thats why the alert closed itself

I know this is late, but might help somebody in the future

@moazam1 The option has been changed to

swal({
  closeOnClickOutside: false,
});

https://sweetalert.js.org/docs/#closeonclickoutside

This didn't worked.

@moazam1 thanks it did worked for me

@moazam1
$(document).on( "click",".fb-share", function(e){
here you have to add e.preventDefault();
openFbPopup();

it worked for me.
});

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ramysa picture ramysa  路  10Comments

kentmw picture kentmw  路  15Comments

srikanthcx picture srikanthcx  路  15Comments

daybaryour picture daybaryour  路  13Comments

fabianchoxD picture fabianchoxD  路  16Comments