Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 Modal Hide Not Working

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>
like image 980
user3693281 Avatar asked Nov 24 '17 01:11

user3693281


People also ask

How do I hide modals in Bootstrap?

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') .

How do I disable modal backdrop?

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 .

How do I stop a Bootstrap modal from closing?

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.

What is data dismiss modal?

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 .


1 Answers

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');
    })
}
like image 136
dferenc Avatar answered Sep 27 '22 18:09

dferenc