Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript callback function for confirm bootbox [duplicate]

I am using bootbox for confirm alert box that I alert to user. It is called from one js file and I have a common function where it is created. How can I use callback function to get the result of the confirm dialog box:

So my js is as below:

 var message = "Do you want to close?";

 loadConfirmAlert(message);
 if (result) {
     //do stuff
 }

The loadCofirmAlert function in another js file then is as below:

var loadCofirmAlert = function (message) {

    bootbox.confirm(message, function (result) { });
}

what I am unsure off is how to pass this result value back to the calling function?

like image 385
Ctrl_Alt_Defeat Avatar asked Mar 17 '26 10:03

Ctrl_Alt_Defeat


1 Answers

Try it like this

var message = "Do you want to close?";

loadConfirmAlert(message,function(result){
    if (result) {
        //do stuff
    }
});

var loadCofirmAlert = function (message,callback) {
    bootbox.confirm(message, function (result) { 
        callback&&callback(result);
    });
}

UPDATE

Where

callback&&callback(result)

Is just a shorter version of

if (callback)callback(result); //if callback defined, call it

You can also use checking if callback is a function by using

(typeof(callback)==="function")&&callback(result)
like image 123
Jevgeni Avatar answered Mar 19 '26 00:03

Jevgeni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!