Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change backdrop to static for open bootstrap modal

Can I change backdrop to 'static' while my modal is open?

I have modal with form submit button. When I click this button I show loading spinner on my modal and that is the moment when I want to change backdrop to static

I tried $('#myModal').modal({backdrop: 'static', keyboard: false}), but I can still close my modal by clicking the backdrop or using escape button.

Second step should be changing backdrop back to true, but I didn't try this yet, because first I need to set backdrop to static.

I could set backdrop to static on modal show, but I want to avoid this and change it after submit.

Any ideas?

like image 796
Exik Avatar asked Aug 04 '16 12:08

Exik


1 Answers

The simplest method I've come up with is attaching to the hide.bs.modal event and calling preventDefault(). This doesn't technically set the backdrop to static, but it achieves the same effect in a toggleable manner.

let $myModal = $('#myModal');

function preventHide(e) {
    e.preventDefault();
}

// if you have buttons that are allowed to close the modal
$myModal.find('[data-dismiss="modal"]').click(() => 
    $myModal.off('hide.bs.modal', preventHide));

function disableModalHide() {
    $myModal.on('hide.bs.modal', preventHide);
}

function enableModalHide() {
    $myModal.off('hide.bs.modal', preventHide);
}

(Disclaimer, I didn't test making buttons allowed to hide the modal, as that wasn't my scenario. If it doesn't work, just call .modal('hide') from the click() callback.)

like image 69
dx_over_dt Avatar answered Nov 10 '22 08:11

dx_over_dt