Disable click outside of bootstrap modal area to close modal
⚡TLDR
To lock a Bootstrap modal so that it cannot be dismissed through mouse clicks outside the modal window, set the backdrop option to 'static':
Add the keyboard option and set it to false to prevent the usage of the Escape key to close the modal:
Now only an explicit action can dismiss the modal, such as invoking another JavaScript command or the user clicking a close button inside the modal.
Navigating multiple modal scenarios
Utilizing HTML attributes
For those who favor HTML over JavaScript for modal customization:
Handling multiple modals
If you are juggling multiple modals on a single page, here's how you can handle their behavior individually:
Overriding default behavior conditionally
Sometimes we need to override the default behavior conditionally. Use the hide.bs.modal event to control when the modal should close:
Common issues and easy fixes
- Persistent Modals: Double-check the backdropandkeyboardvalues. A mistyped value might allow the modal to be dismissed.
- Conflicting Modals: Ensure you're targeting the right modal. I mean, you don’t want to send an email to the wrong person, right?
- Calibri vs Comic Sans: Always remember, Bootstrap JavaScript and third-party scripts are like Calibri and Comic Sans, they often don’t mingle well. Use the .off()method to unbind old event handlers before applying the.on().
Linked
Was this article helpful?
