Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bootbox the show event is not fired

I'm not able to catch the show event for the bootbox.confirm dialog in order to do some css adjustments to the dialog before showing it.

I tried things like the followings but didn't succeed:

$(document).on("show.bs.modal", function (event) {...});
$(document).on("show", function (event) {...});
$(document).on("show", ".bootbox", function (event) {...});
like image 424
alloyoussef Avatar asked Apr 21 '14 13:04

alloyoussef


People also ask

What is a Bootbox and how it is generated?

js is a small JavaScript library which allows you to create programmatic dialog boxes using Bootstrap modals, without having to worry about creating, managing, or removing any of the required DOM elements or JavaScript event handlers.

How do I close Bootbox dialog?

Pressing the ESC key or clicking close () dismisses the dialog and invokes the callback as if the user had clicked the Cancel button. Prompt dialogs require a callback function.


2 Answers

This should work for you using Bootbox 4.x.x and Bootstrap 3.x.x:

var box = bootbox.dialog({
  message: '',
  title: '',
  buttons: {},
  show: false
});

box.on("shown.bs.modal", function() {
  alert('it worked!');
});

box.modal('show');

or like this:

$(document).on("shown.bs.modal", function (event) {...});
like image 177
codephobia Avatar answered Sep 21 '22 02:09

codephobia


I would also prefer to use a global function like:

function bootbox_confirm(msg, callback_success, callback_cancel) {
    var d = bootbox.confirm({message:msg, show:false, callback:function(result) {
        if (result)
            callback_success();
        else if(typeof(callback_cancel) == 'function')
            callback_cancel();
    }});

    d.on("show.bs.modal", function() {
        //css afjustment for confirm dialogs
        alert("before show");
    });
    return d;
}

and call it this way:

bootbox_confirm("my message", function(){alert('ok')}, function(){alert('cancel')}).modal('show');

or when no logic for cancel:

bootbox_confirm("my message", function(){alert('ok')}).modal('show');
like image 30
alloyoussef Avatar answered Sep 17 '22 02:09

alloyoussef