I am looking for a way to make this modal persistent once it show up. As it's presented here, the user can close it with a simple click outside of the div.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(window).load(function(){
$('#myModal').modal('show');
});
});
</script>
</body>
</html>
Is there a way to block this modal so it's still there even with a mouse click outside of it?
To "persist the modal" (aka prevent hiding) hook into the hide event and return false to prevent hiding.
$("#myModal").on('hide.bs.modal', function () {
return false
});
Here's a full code example.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Persistent Modal</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">My Persistent Modal</h4>
</div>
<div class="modal-body">
<p>I'm here for good. Can't hide me.</p>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(window).load(function(){
$('#myModal').modal('show');
});
$("#myModal").on('hide.bs.modal', function () {
return false
});
});
</script>
</body>
</html>
Edit: To hide the modal, redefine the response to the hide event before calling hide()
. The following function does this. You can execute the function whenever is appropriate (e.g. on a button click).
function hideMyModal () {
$("#myModal").on('hide.bs.modal', function () { });
$("#myModal").hide()
}
Something like this ought to get you started. You can fiddle around with different values like color, timing, etc.
html, body {
margin: 0;
background: #878787;
}
h2 {
text-align: center;
}
.modal-content {
position: absolute;
top: 10px;
animation: modal 300ms linear;
border: 1px solid black;
border-radius: 5px;
text-align: center;
background: #fff;
width: 90%;
left: 10px;
box-shadow: 2px 2px 1px #5f5f5f;
opacity: 0;
animation-fill-mode: forwards;
animation-delay: 500ms;
}
@keyframes modal {
0% { opacity: 0; top: 0px}
5% { z-index: 2; }
100% { opacity: 1; z-index: 2; top: 10px}
}
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
</div>
</div>
</div>
</div>
Is code-only answer acceptable?
BootstrapDialog.show({
title: 'Modal Header',
message: 'Some text in the modal.',
closeByBackdrop: false // <- This prevents closing the dialog by clicking outside
});
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.9/css/bootstrap-dialog.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.9/js/bootstrap-dialog.js"></script>
More examples please see here http://nakupanda.github.io/bootstrap3-dialog/