Bootstrap modal backdrop = 'static' not working

João Paulo picture João Paulo · Dec 23, 2015 · Viewed 23.1k times · Source

First, I open my modal using this:

$('#myModal').modal('show');

Then, in another situation, I need that this same modal does not close when pressing ESC/clicking outside, so I use this:

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
})

But once I open my modal by the first method, the second one doesn't work. Any hints?

How can I force backdrop value switch to work?

Answer

Daniele Piccioni picture Daniele Piccioni · Feb 1, 2017

I found a workaround for this issue.

Once the modal has been hidden bootstrap data still remains on it. To prevent that I had the following:

$('#myModal').modal('show'); //display something
//...

// if you don't want to lose the reference to previous backdrop
$('#myModal').modal('hide'); 
$('#myModal').data('bs.modal',null); // this clears the BS modal data
//...

// now works as you would expect
$('#myModal').modal({backdrop:'static', keyboard:false});