1

How to call another function on confirm box return value.

Below is the code I am working with, here it goes to else part only. And when I click confirm box 'yes' button its not calling the function abc. TIA.

jQuery :-

      var result = confirm('Are you sure');
            if (result) {
             abc(); //another jquery function call
            }
            else {               
            }

I have also tried writing on browser console but it writes false only.

var result = confirm('Are you sure');
if (result) {
  console.log('true');
} else {
  console.log('false');
}

I am using sweet alert plugin, may be getting problem from that. My confirm function is given below.

    function confirm(title) {
        var returnType = false;
        swal({
            title: "Warning",//"Are you sure?",
            text: title,
            //text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes",
            cancelButtonText: "No",
            closeOnConfirm: true,
            closeOnCancel: true
        },
         function (isConfirm, id) {
             if (isConfirm) {
                 //window.location.href = href;
                 returnType = true;
                 //swal("Deleted!", "Record has been deleted.", "success");
             } else {
                 //$('div').click();
                 //$('#' + clientid).blur();
                 returnType = false;
                 //swal("Cancelled", "Your imaginary file is safe :)", "error");
             }
         }
      );
        return returnType;
    }
Sunil Chaudhary
  • 397
  • 1
  • 9
  • 31

1 Answers1

2

You can set a [function] callback as a second parameter in this case. This is helpful if in case you wanted to pass a certain [parameter] for your [function] callback

// fnCallback = 'function callback you have set'
function confirm(title, fnCallBack) {
    var returnType = false;
    swal({
            title: "Warning",//"Are you sure?",
            text: title,
            //text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes",
            cancelButtonText: "No",
            closeOnConfirm: true,
            closeOnCancel: true
        },
        function (isConfirm, id) {
            if (isConfirm) {
                // Call [fnCallback] method with your [returnType] value (e.g true)
                returnType = true;
                fnCallBack(returnType);
            } else {
                // Call [fnCallback] method with your [returnType] value (e.g false)
                returnType = false;
                fnCallBack(returnType);
            }
        }
    );

    return returnType;
}

Then when you call the [confirm] function / method, You need to assign two parameters (title and the function itself). For now, we will assign it [anonymously] with a given returnType parameter.

$('.dom-element').click(function() {
    confirm('Invalid data found', function(returnType) {
        // If the returnType given from the [confirm] method
        // is false, call another [swal] method
        if (returnType !== true) {
            setTimeout(function() {
                swal("Cancelled", "Your imaginary file is safe :)", "error");
            }, 200);

            return false;
        }

        // If its true, then:
        setTimeout(function() {
            swal("Deleted!", "Record has been deleted.", "success");
        }, 200);

        return true;

    });
});

This might help you if in case you need to pass some values in the process. Hope this helps.

P.S : If in case you will return the [sval] plugin's alert message only, You can call the [setTimeout] async function in the [confirm] function instead of using a [function] callback as illustrated above.

Marylyn Lajato
  • 1,171
  • 1
  • 10
  • 15