Get value from bootbox confirm callback function

user3351236 picture user3351236 · Sep 15, 2014 · Viewed 9.1k times · Source

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);                    
 });

Answer

chridam picture chridam · Sep 15, 2014

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