0

Sweet-alert not returning the true or false after calling this get_alert() function please suggest some suggestions how could we able to work this

function get_alert() {
  $('#removeactive').on('click', function(e) {
    e.preventDefault();
    var message = $(this).data('confirm');

    //pop up
    swal({
        title: "Are you sure ??",
        text: message,
        icon: "warning",
        buttons: true,
        dangerMode: true,
      })
      .then(function(isConfirm) {
        console.log(isConfirm == true);
        if (isConfirm == true) {
          return true;
        } else {
          return false;
        }
      });
  });
}
<button id="removeactive" data-confirm="Are you sure?" type="button">Click</button>
Rushikesh Ganesh
  • 290
  • 2
  • 16
  • I made you the [mcve] you could have made – mplungjan Dec 03 '20 at 07:09
  • 1
    Most likely [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) but we probably need a bit more context - how are you trying to use this function? – VLAZ Dec 03 '20 at 07:13

1 Answers1

0

You should not need a function to assign an event handler. Also you have not told us when you call get_alert. Calling get_alert will NOT show the alert, only assign the handler

Here I run on load of the page

If the removeactive element is dynamic, you need to change to

$(document).on('click','#removeactive', function(e) {

or better:

$(document).on('click','.removeactive', function(e) {

so any element with that class can call the alert

You also need to remove active where you know the status of the isConfirm

Here is a working example

$(function() { // on page load
  $('#removeactive').on('click', function(e) {
    e.preventDefault();
    var message = $(this).data('confirm');

    //pop up
    swal({
        title: "Are you sure ??",
        text: message,
        icon: "warning",
        buttons: true,
        dangerMode: true,
      })
      .then(function(isConfirm) {
        console.log("confirmed?", isConfirm);
        if (isConfirm) console.log("deleting"); // here you delete
        else console.log("cancelled"); // here you do whatever or nothing
        // You cannot return anything  
      });
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

<button id="removeactive" data-confirm="This will remove the active widget from the sprocket" type="button">Click</button>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • i am calling same function into anther function its not returning anything – Rushikesh Ganesh Dec 03 '20 at 07:15
  • You did not post enough information - you cannot "call the function" you need to run the assignment when loading the page. If your `removeactive` is loaded dynamically you can only have one of them and you need to delegate (IDs need to be unique) – mplungjan Dec 03 '20 at 07:17
  • get_alert() i am calling into anther function – Rushikesh Ganesh Dec 03 '20 at 07:17
  • You should not need a function to assign an event handler. Also you have not told us when you call get_alert. Calling get_alert will NOT show the alert, only assign the handler – mplungjan Dec 03 '20 at 07:19