Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How to hide a Bootstrap 5 modal window in pure JavaScript ?

Hello,

In my app I'm using Bootstrap 5 modal windows to display the forms.

In order to manage validate the fields before to close the form, I want to close / hide the modal window with JavaScript.

In the Bootstrap 5 documentation, it is said that jQuery is not used anymore, so I don't have installed jQuery in my project. Bootstrap 5 proposes to use this code to hide a modal window.

https://getbootstrap.com/docs/5.0/components/modal/#hide
...
document.addEventListener('DOMContentLoaded', () => {
    document.addEventListener('closeModal', () => {
        var modal = new bootstrap.Modal(document.getElementById('authorForm'));
        modal.hide();
    });
});

This doesn't work ... so I wonder if this hide() method is a jQuery method ?

Or perhaps I have forgotten something ...

If you have any idea ;) ...

Thanks a lot.

Vincent

0 likes
13 replies
bobbybouwmann's avatar

Well, the example from the docs should work just fine. hide() is not a jQuery method. It is a method on the modal object from Bootstrap 5.

Can you show your code?

1 like
vincent15000's avatar

@bobbybouwmann Hello thank you for your answer.

Here is my view.

<div>

	// Here is a datatable

	...

	// And here I have the modal form

	<div wire:ignore.self class="modal fade" id="authorForm" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">

        <div class="modal-dialog modal-dialog-centered modal-lg">

            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title">{{ $formTitle }}</h5>
                </div>

                <form wire:submit.prevent="save">

                    <div class="modal-body">
						...
                    </div>

                    <div class="modal-footer">
                        <button wire:click="cancel" type="button" class="btn btn-primary" data-bs-dismiss="modal">Annuler</button>
                        <button type="submit" class="btn btn-secondary">Enregistrer</button>
                    </div>

                </form>

            </div>

        </div>

    </div>

    <script>

        document.addEventListener('DOMContentLoaded', () => {
            document.addEventListener('closeModal', () => {
                var modal = new bootstrap.Modal(document.getElementById('authorForm'));
                modal.hide();
            });
        });

    </script>

</div>

And here is the save method in the controller.

public function save()
{
    $this->validate($this->rules, $this->messages);
    if ($this->updateMode) {
        $this->authorize('update', $this->author);
        $this->author->update();
    } else {
        $this->authorize('create', $this->author);
        $this->author->save();
    }
    $this->author->roles()->sync($this->selectedRoles);
    $this->dispatchBrowserEvent('closeModal');
    $this->initForm();
}
bobbybouwmann's avatar
Level 88

Are you sure the javascript from bootstrap is loaded? Also, what does var modal contain? Is it pointing to the correct element in de DOM?

Anyway, it doesn't make sense to me to use livewire on one hand and then bootstrap for the javascript part. I would personally just build the model in a livewire component and use it from there.

vincent15000's avatar

@bobbybouwmann Thank you for answer ...

In my bootstrap.js file, I have this, so I think that the javascript from bootstrap is loaded.

window._ = require('lodash');
import * as bootstrap from 'bootstrap';
window.bootstrap = bootstrap;
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

What you tell me is interesting ...

I don't necessarily want to use bootstrap for the javascript part. Why I try to use it is because I use Bootstrap as CSS framework and I want that the modal window (from the CSS framework) does not close when I click on the save button (in case of the data are not validated by the rules). So I have to close it manually after checking if the data are validated, and the I save and I close the modal window.

I have looked for a solution to do this without using JavaScript, but I don't understand how I can do. If I could do that another way with only Livewire, it could be great, but I don't know how.

What do you exactly mean by saying "I would personally just build the model in a livewire component and use it from there." ?

vincent15000's avatar

@bobbybouwmann Oh perhaps you mean adding / removing a hidden CSS attribute to the modal window, so showing and hiding it only with Livewire ?

vincent15000's avatar

@bobbybouwmann Hello ... I have at least found the solution.

This does not work.

document.addEventListener('DOMContentLoaded', () => {
    document.addEventListener('closeModal', () => {
        var modal = new bootstrap.Modal('#typeForm');
        modal.hide();
    });
});

And this works.

document.addEventListener('DOMContentLoaded', () => {
    var modal = new bootstrap.Modal('#typeForm');
    document.addEventListener('closeModal', () => {
        modal.hide();
    });
});

I just had to put the modal variable outside the event listener.

1 like
vincent15000's avatar

@bobbybouwmann Yes ... it was only a little code to change, but I didn't see it right now. But you helped me by saying to me that it should work, so I have checked and tested several things. And finally I have noticed why it didn't work. Thank you @bobbybouwmann ;).

Dmitry_Allread_Return's avatar

@vincent15000 you're crazy bro)) didn't help anything but your solution. That's weird that the show() method works just fine inside any event listeners, but this ugly hide() doesnt! Thx man for your answer!

1 like
aboozar_k's avatar

hi.

I try Laravel 9 and Bootstrap 5 without a jQuery for using Modal Bootstrap.

I doing delete the data: my modal id is = deleteModal.

  • 1- add wire:ignore.self in Div modal for open and stay open modal.

  • 2- at the end of function to deleted data insert

    $this->dispatchBrowserEvent('closeModal');

    that means call Event "closeModal" .

  • 3- add @stack('script') for listening to script.

  • 4- add

@push('script')
    <script>
        window.addEventListener('closeModal', event => {
            document.querySelector('#deleteModal').style.display = "none";
            document.querySelector('.modal-backdrop').remove();
        })
    </script>
@endpush

for close modal.

the div by class "modal-backdrop" at the end of html page that is dark at the background the Modal.

I hope help another:

my email: [email protected]

1 like
joyltonmaciel's avatar

@aboozar_k Hello, I follow your instructions and after close the modal no mouse, no keyboard, no scroll bar works properly.

I have to reopen the modal clicking twice on open modal button, and clicking the close button (which has the data-bs-dismiss="modal") to close again the modal and to the page return to normal.

It seems something more needs to be removed like the .modal-backdrop... I don't know!

I am using Laravel 11, bootstrap 5 and livewire 3.

do you have an idea about what can be causing this problem?

1 like
Snapey's avatar

@joyltonmaciel don't mess with the DOM, use these jquery functions

$('#myModal').modal('toggle');
$('#myModal').modal('show');
$('#myModal').modal('hide');
1 like

Please or to participate in this conversation.