flowbite: Allow access to the object instances created via the data attributes API inside JS (usage example problems with the modal component)
Describe the bug if open modal with javascript, i can’t close modal with javascript helper or data-modal-toggle=“”
To Reproduce Steps to reproduce the behavior:
- Go to official site: https://flowbite.com/docs/components/modal/#methods
- open modal with javascript from chrome console
new Modal(document.getElementById('defaultModal'), null).show();- Close modal with (X) icon inside modal, not close modal!
- no javscript error
Expected behavior close correctly modal
Screenshots

Desktop (please complete the following information):
- OS: Ubuntu
- Browser chrome
- Version 1.4.1
Smartphone (please complete the following information):
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 56 (11 by maintainers)
hello, unfortunately we still do not understand the basic problem. use javascript to close the modal we agree, but if you open it with a normal toggle from a button and then you want to manage the closure with javascript, the reference to “modal” is lost (not to mention the possibility of having more modals open at the same time, how do you manage the reference? the problem still has not been solved and the management of events and the library should be improved to give the possibility to manage the modal in both ways (the correct motion in the management of the modal is exactly as bootstrap does https://getbootstrap.com/docs/5.2/components/modal/). in a nutshell, the backdrop must have the reference to the modal in some way, to be able to fully manage it!
Hey everyone!
Flowbite v2.0 fixes this issue by bringing a brand new Instances Manager.
Please read more about it here:
https://flowbite.com/docs/getting-started/javascript/ https://github.com/themesberg/flowbite/releases/tag/v2.0.0
Cheers, Zoltan
Hi guys,
a quick fix for me, so hope it help you too
Hey everyone,
Just want to let you all know that I will provide a solution for this in the upcoming major version together with introducing prefixes - please stand by on this one. Appreciate the patience and if any one of you want to make a PR for this I’ll be more than happy to have a look at it and even collaborate on it.
I’m currently working on the autocomplete component and right after that I’ll prepare for these two new changes.
Cheers, Zoltan
Hi everyone, I’m facing this issue and I’m proposing a fix here https://github.com/themesberg/flowbite/pull/274.
simple, this is an example code to get the problem out: https://jsfiddle.net/2jkws153/
This exactly.
@KinG-InFeT change to “placement”:“center” in the options
I circumvented it as follows.
@rmelchner This is a good solution for this particular issue. I added one comment but otherwise, I hope your PR is accepted.
I think there are still some underlying issues resulting from side effects of the init functions (initModals, initDrawers, etc.) being called automatically regardless of whether initFlowbite() is called. For example:
@zoltanszogyenyi thank you - i updated my PR https://github.com/themesberg/flowbite/pull/649
I tested it and it works as expected.
@KinG-InFeT you can check again was easier than expected.
For all the people that need a quick fix:
The important part is onHide.
Any updates to allowing access to the instances that are being created via the data attributes? Thanks!
creating a modal object with options and using modal.show() and modal.hide() instead of the default data-modal-toggle, worked for me, tnks 😄
Hey everyone,
I know that this has been long overdue, but with the
v1.6.0update and introducing TypeScript I have finally addressed this issue by changing thedata-modal-toggletodata-modal-targetwhich will be initialised only once, and the you can usedata-modal-toggle/show/hide={modalId}to search for the modal instance and toggle, show or hide it.Here’s a breakdown of the code that does this:
The update will arrive shortly and unfortunately we will have some breaking changes because
data-modal-togglewill no longer initialise a modal object, onlydata-modal-target. The code will not break though.This also fixed stacking up the event listeners for when click outside of the modal + escape button.
The functionality can be tested on this branch: https://github.com/themesberg/flowbite/tree/1.6.0
@csoutham merged the PR. I think it’s safe to close this issue 😃
hi @zoltanszogyenyi , thank you for reply, after hide, the background continue to appair