Reputation: 5901
I am creating a modal window using Twitter Bootstrap. The default behavior is if you click outside the modal area, the modal will automatically close. I would like to disable that -- i.e. not close the modal window when clicking outside the modal.
Can someone share jQuery code to do this?
Upvotes: 585
Views: 354155
Reputation: 1340
If you are using Bootstrap 5 or later, it's recommended to use data-bs-backdrop
and data-bs-keyboard
. If you are using Bootstrap 4 or an earlier version, you can use data-backdrop
and data-keyboard
. Ensure that you are using the correct attribute based on the version of Bootstrap you are working with.
v5
data-bs-backdrop="static" data-bs-keyboard="false"
v4
data-backdrop="static" data-keyboard="false"
https://getbootstrap.com/docs/5.2/components/modal/#static-backdrop
Upvotes: 0
Reputation: 283
If you are using bootstrap 5 or above, use the below code in modal div
<div class="modal fade customModal customModalForm" id="editPartySetting" data-bs-keyboard="false" data-bs-backdrop="static" tabindex="-1" aria-labelledby="editSettings"
aria-hidden="true" >
This will restrict user, now popup won't get closed by 'Esc' button and by clicking outside.
Upvotes: 0
Reputation: 2952
Nobita's answer works for single cases, but I have like 20-30 modals on an old site and was looking for an answer that set this for every modal opened with bootstrap.
You can target the event namespace for the modal and add in default settings for every bootstrap modal.
You might not want to do this for every modal but in case you did, there's no need to statically specify each modal call.
$(document).on('click.modal.data-api', '[data-toggle="modal"]', function (e) {
var $this = $(this)
var href = $this.attr('href')
var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7
var option = $target.data('modal') ? 'toggle' : $.extend({ remote:!/#/.test(href) && href }, $target.data(), $this.data(), {backdrop: 'static', keyboard: false})
e.preventDefault()
$target
.modal(option)
.one('hide', function () {
$this.focus()
})
})
In my case, I added {backdrop: 'static', keyboard: false}
to the option structure.
Upvotes: 0
Reputation: 199
Just set static
for backdrop
option (backdrop: 'static'). And prevent closing the modal by pressing Esc
from keyboard
you have to set false
for keyboard
option (keyboard: false)
So, the code will be.
var jq = jQuery.noConflict();
jq(document).ready(function(){
jq('#exampleModal').modal({backdrop: 'static', keyboard: false});
});
Upvotes: 0
Reputation: 445
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Launch static backdrop modal
</button>
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
...
</div>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">
Launch static backdrop modal
</button>
<div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
...
</div>
</div>
Upvotes: 5
Reputation: 31
When backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it. use data-bs-backdrop="static" data-bs-keyboard="false" for bootstrap 5 and data-backdrop="static" data-keyboard="false" for bootstrap 4
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap 4 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container p-5" >
<div class="row text-center">
<div class="col-md-6">
<!-- Button trigger modal -->
When backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it. use data-bs-backdrop="static" data-bs-keyboard="false"
<button type="button" class="btn btn-primary my-4" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Launch static backdrop 5 modal
</button>
<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<!-- Button trigger modal -->
When backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it. use data-backdrop="static" data-keyboard="false"
<button type="button" class="btn btn-primary my-4" data-toggle="modal" data-target="#exampleModal">
Launch static backdrop 4 modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"
data-backdrop="static" data-keyboard="false">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap 5 Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Bootstrap 4 -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>
</html>
Upvotes: 0
Reputation: 1116
The updated syntax according to bootstrap 5 is as followed.
Reference Link
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" >
Upvotes: 2
Reputation: 31
Attribute names have changed in Bootstrap 5. You can use the following :
data-bs-backdrop="static" data-bs-keyboard="false"
Upvotes: 3
Reputation: 127
The solution presented as an answer does not work, what is wrong?
$(document).ready(function(){
$('.modal').modal('show');
$('.modal').modal({
backdrop: 'static',
keyboard: false
})
});
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/index.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<div class="text-right"><button type="button" class="btn btn-primary">print</button></div>
</div>
</div>
</div>
</div>
</body>
</html>
Upvotes: 0
Reputation: 46
$(document).ready(function(e){
$("#modalId").modal({
backdrop: 'static',
keyboard: false,
show: false
});
});
"backdrop:'static'" will prevent closing modal when clicking outside of it; "keyboard: false" specifies that the modal can be closed from escape key (Esc) "show: false" will hide the modal when the page has finished loading
Upvotes: 0
Reputation: 1776
Try main line with:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="verifyModalLabel" aria-hidden="true">
Upvotes: 0
Reputation: 526
To Update the backdrop state in Bootstrap 4.1.3 after the modal have been Display, We used the following line from Bootstrap-Modal-Wrapper plugin. Plugin Repository code reference.
$("#yourModalElement").data('bs.modal')._config.backdrop = (true : "static");
Upvotes: 0
Reputation: 2339
Yes, you can do it like this:
<div id="myModal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true"
data-backdrop="static" data-keyboard="false">
Upvotes: 33
Reputation: 499
Override the Bootstrap ‘hide’ event of Dialog and stop its default behavior (to dispose the dialog).
Please see the below code snippet:
$('#yourDialogID').on('hide.bs.modal', function(e) {
e.preventDefault();
});
It works fine in our case.
Upvotes: 37
Reputation: 33962
Kind of like @AymKdn's answer, but this will allow you to change the options without re-initializing the modal.
$('#myModal').data('modal').options.keyboard = false;
Or if you need to do multiple options, JavaScript's with
comes in handy here!
with ($('#myModal').data("modal").options) {
backdrop = 'static';
keyboard = false;
}
If the modal is already open, these options will only take effect the next time the modal is opened.
Upvotes: 22
Reputation: 3897
If you already have initialized the modal window, then you may want to reset the options with $('#myModal').removeData("modal").modal({backdrop: 'static', keyboard: false})
to make sure it will apply the new options.
Upvotes: 47
Reputation: 23713
I believe you want to set the backdrop value to static. If you want to avoid the window to close when using the Esc key, you have to set another value.
Example:
<a data-controls-modal="your_div_id"
data-backdrop="static"
data-keyboard="false"
href="#">
OR if you are using JavaScript:
$('#myModal').modal({
backdrop: 'static',
keyboard: false
});
Upvotes: 723
Reputation: 222
<button type="button" class="btn btn-info btn-md" id="myBtn3">Static
Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal3" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Static Backdrop</h4>
</div>
<div class="modal-body">
<p>You cannot click outside of this modal to close it.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-
dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$("#myBtn3").click(function(){
$("#myModal3").modal({backdrop: "static"});
});
});
</script>
Upvotes: 1
Reputation: 41
The best I found is add this code to the link
<!-- Link -->
<a href="#mdl" role="button" data-backdrop="static" data-keyboard="false" data-toggle="modal" id_team="" ></a>
<-- Div -->
<div id="mdl" class="modal hide fade" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static"></div>
Upvotes: 4
Reputation: 712
Doing that is very easy nowadays. Just add:
data-backdrop="static" data-keyboard="false"
In your modal divider.
Upvotes: 3
Reputation: 1610
You can set default behavior of modal popup by using of below line of code:
$.fn.modal.prototype.constructor.Constructor.DEFAULTS.backdrop = 'static';
Upvotes: 2
Reputation: 13908
If you want to conditionally disable the backdrop click closing
feature. You can use the following line to set the backdrop
option to static
during runtime.
Bootstrap v3.xx
jQuery('#MyModal').data('bs.modal').options.backdrop = 'static';
Bootstrap v2.xx
jQuery('#MyModal').data('modal').options.backdrop = 'static';
This will prevent an already instantiated model with backdrop
option set to false
(the default behavior), from closing.
Upvotes: 2
Reputation: 61
As D3VELOPER says, the following code resolve it:
$('#modal').removeData('bs.modal').modal({backdrop: 'static', keyboard: false});
I'm using both jquery & bootstrap and simply removeData('modal')
don't work.
Upvotes: 6
Reputation: 2017
Well, this is another solution that some of you guys might be looking for (as I was..)
My problem was similar, the modal box was closing while the iframe I had inside was loading, so I had to disable the modal dismiss until the Iframe finishes loading, then re-enable.
The solutions presented here were not working 100%.
My solution was this:
showLocationModal = function(loc){
var is_loading = true;
if(is_loading === true) {
is_loading = false;
var $modal = $('#locationModal');
$modal.modal({show:true});
// prevent Modal to close before the iframe is loaded
$modal.on("hide", function (e) {
if(is_loading !== true) {
e.preventDefault();
return false
}
});
// populate Modal
$modal.find('.modal-body iframe').hide().attr('src', location.link).load(function(){
is_loading = true;
});
}};
So I temporarily prevent the Modal from closing with:
$modal.on("hide", function (e) {
if(is_loading !== true) {
e.preventDefault();
return false
}
});
But ith the var is_loading that will re enable closing after the Iframe has loaded.
Upvotes: 1
Reputation: 321
You can disable the background's click-to-close behavior and make this the default for all your modals by adding this JavaScript to your page (make sure it is executed after jQuery and Bootstrap JS are loaded):
$(function() {
$.fn.modal.Constructor.DEFAULTS.backdrop = 'static';
});
Upvotes: 11
Reputation: 9549
Just set the backdrop
property to 'static'
.
$('#myModal').modal({
backdrop: 'static',
keyboard: true
})
You may also want to set the keyboard
property to false
because that prevents the modal from being closed by pressing the Esc key on the keyboard.
$('#myModal').modal({
backdrop: 'static',
keyboard: false
})
myModal
is the ID of the div that contains your modal content.
Upvotes: 314
Reputation: 1493
Just add these two things
data-backdrop="static"
data-keyboard="false"
It will look like this now
<div class="modal fade bs-example-modal-sm" id="myModal" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
It will disable the escape button and also the click anywhere and hide.
Upvotes: 14
Reputation: 1014
In case anyone comes here from Google trying to figure out how to prevent someone from closing a modal, don't forget that there's also a close button on the top right of the modal that needs to be removed.
I used some CSS to hide it:
#Modal .modal-header button.close {
visibility: hidden;
}
Note that using "display: none;" gets overwritten when the modal is created, so don't use that.
Upvotes: 2
Reputation: 5099
You can also include these attributes in the modal definition itself:
<div class="modal hide fade" data-keyboard="false" data-backdrop="static">
Upvotes: 225