Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from bootbox confirm callback function

console.log(r); gets nothing. How can I pass the result to r variable when using bootbox

function jconfirm(m){
    bootbox.confirm({
        message: m,
        buttons: {
            'cancel': {
                label: 'No'                       
            },
            'confirm': {
                label: 'Yes',                   
            }
        },
        callback: function(result) {               
           return result;
        }
    });
}
 jconfirm('Do you really ....', function(r){
    console.log(r);                    
 });
like image 638
user3351236 Avatar asked Jun 15 '26 07:06

user3351236


2 Answers

You could try this approach:

var jconfirm = function (message, callback) {
    var options = {            
        message: message
    };
    options.buttons = {
        cancel: {
            label: "No",
            className: "btn-default",
            callback: function(result) {
                callback(false);
            }
        },
        main: {
            label: "Yes",
            className: "btn-primary",
            callback: function (result) {
                callback(true);
            }
        }
    };
    bootbox.dialog(options);
};


$('#delete').on('click', function (e, confirmed) {
    if (!confirmed) {
        e.preventDefault();
        jconfirm("Do you really ....", function (r) {
            console.log(r);
            if (r) {
                $('#delete').trigger('click', true);
            }
        });
    }
});
$('#form').submit(function (e) {
    //do your validation or whatever you need to do before submit
});

JSFiddle

like image 69
chridam Avatar answered Jun 16 '26 21:06

chridam


         //You may also can use this is working for me.                
                //Call function
                confirm_boot('Delete','Are you sure? ', function(result) {
                                    console.log("Confirmed? " + result);
                                    });

                                 //define function
                            var confirm_boot = function(title, msg,callback) {
                            bootbox.confirm({
                                    title: title,
                                    message: msg,
                                    buttons: {
                                        cancel: {
                                            label: '<i class="fa fa-times"></i> Cancel',
                                        },
                                        confirm: {
                                            label: '<i class="fa fa-check"></i> Confirm',
                                        }
                                    },
                                    callback: function(result) {
                                        console.log('This was logged in the callback: ' + result); 
                                        callback(result);
                                    }
                                });

                            }
like image 34
Dheeraj singh Avatar answered Jun 16 '26 22:06

Dheeraj singh



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!