Skip to content
Advertisement

Bootstrap modal UncaughtType Error on modal loading

I am using BS modal in my project. I am using a button to open the model but as I open the modal I get this error in my console.

Error:

modal.js:418 Uncaught TypeError: Cannot read properties of null (reading 'hide')
    at HTMLButtonElement.<anonymous> (modal.js:418:23)
    at HTMLDocument.s (event-handler.js:119:21)

HTML:

<div class="card my-2 mx-2 mt-4" style="width: 18rem;">
<button class="btn btn-primary" data-bs-target="#mymodal" data-bs-toggle="modal"  
style="position: absolute; bottom: 0; right: 0">Get Quote</button>
</div>

Modal:

<div class="modal" id="mymodal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
DO SOMETHING
</div>
</div>
</div>
</div>

I am using Bootstrap5 CDNs:

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

Advertisement

Answer

It could be a bug in Bootstrap5 but I solved by adding the fade class to the modal.

Here’s how I did it.

<div class="modal fade" id="mymodal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                DO SOMETHING
            </div>
        </div>
    </div>
</div>
Advertisement