22

I'm using bootbox to show dialog.

If I use bootbox.confirm, bootbox.alert or bootbox.prompt, when pressing escape key or clicking outside the dialog, the dialog closed as expected

but when using bootbox.dialog, when I click outside the dialog or pressing escape key, the dialog doesn't close, how to make it behave as other dialog do?

var box = bootbox.dialog({
    show: false,
    backdrop: true,
    animate: false,
    title: 'Bla',
    message: 'bla bla bla',
    buttons: {
        cancel: {
            label: 'Cancel',
            className: 'btn-warning'
        },
        save: {
            label: 'Parse',
            className: 'btn-success',
            callback: function () {
                // handling with ajax
                return false;
            }
        }
    }
});
box.modal('show');
JoriO
  • 1,050
  • 6
  • 13
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 2
    you can use the onEscape: function(){//callback } to do the trick with the options, you have given, give this as well and just say return in that onEscape: function(){return;} done http://bootboxjs.com/documentation.html – Ankur Verma Feb 25 '15 at 16:40

6 Answers6

39

This should do it. Please note this has only been tested on v3. using bootstrap 2.3.2

$(document).on('click', '.modal-backdrop', function (event) {
    bootbox.hideAll()
});
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
tibc-dev
  • 973
  • 11
  • 16
19

Add an onEscape callback function, which may have an empty body.

See docs and example.

Basic code:

bootbox.dialog({
   onEscape: function() {},
   // ...
});
bart
  • 7,640
  • 3
  • 33
  • 40
2

To be honest I've never really used modal - it came from a PR a long, long time ago but I've never been convinced of its use case. No good to you now but the method is actually commented as being deprecated in v3.0.0 and will probably actually be removed in future versions - it just doesn't really fit (to me) what Bootbox was created for and as other methods have been tweaked, improved and tested it's sat there somewhat neglected.

But you can do something like this

$(document).keyup(function(e) {
  if (e.keyCode == 27) {box.modal("hide");}   // esc
});
Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142
2

For those looking to close a single bootbox modal when you have multiple modals open I have found the following to work without breaking the others:

dialog.find(".bootbox-close-button").trigger("click");
apc
  • 5,306
  • 1
  • 17
  • 26
1

In version 3, with dialog, backdrop being true only works when onEscape is true as well - so you just need to set both to true, e.g.

bootbox.dialog({message:'Message', title:'Title', backdrop:true, onEscape:true})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js"></script>
AndyS
  • 725
  • 7
  • 17
1

I tried other answers here and they didn't work for me. I'm not sure if it had to do with the specific version of bootbox I was using or some other reason, but I just rolled my own solution to:

  • only close the last modal that was opened (i.e. for nested modals, don't close all of them)
  • selectively choose which modals will close on outside click and which will not
  • work on all of the modal flavors (dialog, alert, confirm etc)
  • not require me to change libraries or library versions

by doing the following:

function hideDialogOnOutsideClick(d) { // d = bootbox.dialog(...)
  d[0].addEventListener('click', function(e) {
    if(e.target == d[0])
      $(d).modal('hide');
    e.stopImmediatePropagation();
    return false;
  });
}

which is used like so:

var d = bootbox.dialog(...) // or alert, confirm etc
hideDialogOnOutsideClick(d);
mwag
  • 3,557
  • 31
  • 38