Bootstrap Modal and jQuery Event Handlers: Does data-bs-dismiss='modal' destroy the instance or just hide it?
0 reputation · 08 Aug 2024, 12:57 UTC
0 reputation · 08 Aug 2024, 12:57 UTC
The goal is to determine whether repeated use of Bootstrap 5.3 Modals triggered by data-bs-dismiss="modal" leaves the modal instance alive when jQuery event handlers are attached to the modal or its backdrop, and whether this interaction can cause a memory leak in long‑running single‑page applications.
Bootstrap 5.3’s v5.3.2 patch removes the backdrop element and detaches its listeners on hide, but the component does not expose a public destroy method, and the specification does not clarify if data-bs-dismiss="modal" should destroy the instance or merely hide it. When custom jQuery listeners are bound to the modal container, they remain unless explicitly removed, creating uncertainty about required cleanup.
What is the exact state of the modal instance after a hide triggered by the attribute? Are jQuery‑bound listeners automatically cleared when the backdrop is removed? Should developers implement a custom dispose routine to guarantee no lingering references in SPAs?
31775 reputation · 08 Aug 2024, 23:45 UTC
data-bs-dismiss="modal" only hides the modal. It calls the modal's hide() method, which removes the backdrop and Bootstrap's own backdrop listeners, but the Modal instance, its config, and its internal listeners stay alive on the .modal element. Nothing is destroyed, and any jQuery handlers you attached to the modal container or backdrop are not cleared automatically.
hide(). There is no option to make it dispose.bootstrap.Modal.getInstance(el) still returns it after the modal is hidden.$(el).on(...) bindings on the modal element survive. Delegated handlers on document or window are completely unaffected by anything the modal does.It depends on your pattern, and this is the distinction that matters:
show.bs.modal without a matching .off() is the classic duplicate-handler bug.Dispose explicitly after the hide animation completes, and unbind your own handlers in the same place:
const el = document.getElementById('myModal');
const instance = bootstrap.Modal.getOrCreateInstance(el);
el.addEventListener('hidden.bs.modal', () => {
$(el).off(); // remove your jQuery handlers on the modal
instance.dispose(); // remove Bootstrap listeners + release instance
el.remove(); // if the markup was injected dynamically
});Guard against double-dispose (if (bootstrap.Modal.getInstance(el))) and never dispose mid-animation or on a modal you intend to reuse.
bootstrap.Modal.getInstance(document.querySelector('.modal')) in devtools — it returns the instance, confirming hide ≠ destroy.dispose() and re-check — it should return null.Modal objects and jQuery handler closures will show up in the delta.This describes Bootstrap 5.3.x behavior; the backdrop-cleanup details have shifted across 5.x patch releases, so verify against your exact version with the devtools check above. If your jQuery handlers are all delegated to document with a selector, they never attach to the modal at all — in that case the only cleanup question is the Bootstrap instance itself.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.