In Bootstrap v3.3.7 the below code works fine. I have recently attempted to upgrade to Bootstrap v4.0.0-beta.2 for some reason it no longer works.
What I am doing is showing a modal div that has as a spinner on it. I then go and load the rest of page and when all done loading the rest of the page I close the modal div. Again worked fine in v3, no longer works in v4. I can however open the console and run $("#divLoading").modal('hide'); and the div goes away.
FIDDLE Boostsrap v4 [BROKE]: https://jsfiddle.net/gc1097oh/
FIDDLE Bootstrap v3 [WORKS]: https://jsfiddle.net/7skoLo2q/
<div id="divMain" class="Main">
<div id="divLoading" class="modal fade">
<div class="loader">
<br />
<br />
loading div actual div has a spinner but not need to show error
</div>
<div class="modal-dialog invisible">
</div>
</div>
</div>
Javascript:
<script type="text/javascript">
$(function () {
showLoading();
//do some work then hide
hideLoading();
});
function showLoading() {
$('#divLoading').modal({
backdrop: 'static',
keyboard: false
});
}
function hideLoading() {
$("#divLoading").modal('hide');
}
</script>
Answer: Use the modal('hide') Method You can simply use the modal('hide') method to hide or close the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('show') and modal('toggle') .
On Options chapter, in the page you linked, you can see the backdrop option. 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 .
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.
modal-header class is used to define the style for the header of the modal. The <button> inside the header has a data-dismiss="modal" attribute which closes the modal if you click on it. The . close class styles the close button, and the .
Modals are created in an asynchronous manner, but you are calling your showLoading()
and hideLoading()
functions in a synchronous way. You can check if the modal has been displayed already in your hideLoading function like so:
function hideLoading() {
$('#divLoading').on('shown.bs.modal', function (e) {
$("#divLoading").modal('hide');
})
}
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