Search code examples
javascripttwitter-bootstrap-3modal-dialoghideconfirm

Ask if user really want to close bootstrap modal


I have a bootstrap modal. When it is closed some instructions are executed. But now I want to ask if user really want to close it (with a confirm window) What can i do?

Here is my code at the moment.

$('#modal').on("hide.bs.modal", function (e) {
    //Instructions to execute when the modal is closed
});

Solution

  • Like this?

    $('#modal').on("hide.bs.modal", function (e) {
       if(confirm("Are you sure, you want to close?")) return true;
       else return false;
    });
    

    Or, just like this:

    $('#modal').on("hide.bs.modal", function (e) {
       if(!confirm("Are you sure, you want to close?")) return false;
    });