On a page, I'm going to have a bootstrap modal with a form that will not be necessary to be all filled in one go.
Is there a way that, when the user hits the close button on the modal, to have a warning popup with confirm close and cancel close?
Passing this option with value 'static' will prevent closing the modal. As @PedroVagner pointed on comments, you also can pass {keyboard: false} to prevent closing the modal by pressing Esc . Save this answer.
How do I stop closing the modal when I click outside? Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.
Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead. Clicking on the modal “backdrop” will automatically close the modal.
The close button can be added by using the simple markup with CSS class. The main container for close button is the <button> tag with . close class.
Assuming you have 2 modals
one for form (first modal) and 2nd for warning to close modal (both modals)
and using bootstrap default modal behaviour data-toggle
and data-target
to invoke the first modal (with form)
Important:
Make sure you add data-backdrop="static"
and data-keyboard="false"
in Form Modal trigger button so it will not be closed when click outside the modal otherwise the whole solution is useless.
Make sure you add data-backdrop="false"
in Warning Modal so there will be only one back-drop for first modal.
Changes:
Remove data-dismiss="modal"
from both Header / Footer Close Button
of Form Modal
Add data-dismiss="modal"
to Warning Modal Cancel Close button
just only to dismiss Warning modal
Add class closefirstmodal
in Form Modal Header / Footer Close button
to invoke the warning modal with jQuery click function and bootstrap modal event listener
Add class confirmclosed
in Warning Modal Confirm close Button
which will use to close both Form / Warning Modals with jQuery click function and hide both Modal via jQuery $('#Modalselector').modal('hide')
Modal's HTML
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#Form" data-backdrop="static" data-keyboard="false">Open Modal</button>
<!-- Modal With Form -->
<div id="Form" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close closefirstmodal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some Form Elements Here</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default closefirstmodal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal With Warning -->
<div id="Warning" class="modal fade" role="dialog" data-backdrop="false">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<p>This Is A Warning Message</p>
<button type="button" class="btn btn-danger confirmclosed">Confirm Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel Close</button>
</div>
</div>
</div>
</div>
JS with B.S modal Event Listener (You can skip the event listener but I prefer this way)
//jQuery and Bootstrap Lib's always comes first
$(document).ready(function () { //Dom Ready
$('.closefirstmodal').click(function () { //Close Button on Form Modal to trigger Warning Modal
$('#Warning').modal('show').on('show.bs.modal', function () { //Show Warning Modal and use `show` event listener
$('.confirmclosed').click(function () { //Waring Modal Confirm Close Button
$('#Warning').modal('hide'); //Hide Warning Modal
$('#Form').modal('hide'); //Hide Form Modal
});
});
});
});
Fiddle example-1
JS without B.S modal Event Listener
$(document).ready(function () {
$('.closefirstmodal').click(function () {
$('#Warning').modal('show');
});
$('.confirmclosed').click(function () {
$('#Warning').modal('hide');
$('#Form').modal('hide');
});
});
Fiddle Example-2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With