Search code examples
jquerytwitter-bootstrapbootbox

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

Solution

  • 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