Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect backdrop click on Bootstrap 3 modal

When using Twitter Bootstrap 3, Is there anyway to know when the modal is closed by a backdrop click ?

like image 978
Tahola Avatar asked Jan 19 '14 11:01

Tahola


People also ask

How do I show modal popups?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do you prevent Bootstrap modal from hiding when clicked outside the content area?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

How do I know if a Bootstrap modal is open?

How check modal is open or not in jQuery? You can simply use the modal('show') method to show or open the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('hide') and modal('toggle') .


2 Answers

The easiest and and functional way in every case-

$(document).click(function (e) {
    setTimeout(function(){
        if (!$('body').hasClass('modal-open')) {
            $("#myModal iframe").attr("src", $("#myModal iframe").attr("src"));
        }
    },1000);
});
like image 149
Partharaj Deb Avatar answered Oct 23 '22 09:10

Partharaj Deb


I can't find any built in function to check what you want.

The only "hacky" way I found is to check the click/keyup event of the document and if the modal is opened call your callback.

Code:

$(document).keyup(function (e) {
    if (e.which == 27 && $('body').hasClass('modal-open')) {
        console.log('esc')
    }
})

$(document).click(function (e) {
    if (e.target === $('.modal-scrollable')[0] && $('body').hasClass('modal-open')) {
        console.log('click')
    }
})

Demo: http://jsfiddle.net/IrvinDominin/7nnUq/

like image 20
Irvin Dominin Avatar answered Oct 23 '22 08:10

Irvin Dominin