bootstrap: modal('show') after a modal('hide') doesn't work
immediately after hiding a modal, if a show call is issued it doesn’t work e.g.
$('#show_modal').click(function(){
$('#test-modal').modal('show')
$('#test-modal').modal('hide')
$('#test-modal').modal('show')
})
Looks like end effect should be a modal dialog shown, but instead modal is hidden with a black screen see it in action here http://jsfiddle.net/anuraguniyal/qzAmP/
For time being I am working around it by issuing a setTimeout of 1000 msec, because looks like bootstrap takes 500 msec to hide modal in a timeout
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Comments: 22 (1 by maintainers)
Commits related to this issue
- gui: Fix modals overlapping and body padding added when saving config changes (ref #9063) Ensure that the previous modal has been fully hidden before proceeding to saving changes. Without this, the "... — committed to tomasz1986/syncthing by tomasz1986 10 months ago
- gui: Fix body padding infinitely increasing due to overlapping modals (ref #9063) Opening and hiding multiple modals at the same time as well as opening a modal before fully hiding the previous one c... — committed to tomasz1986/syncthing by tomasz1986 10 months ago
- gui: Fix body padding infinitely increasing due to overlapping modals (ref #9063) Opening and hiding multiple modals at the same time as well as opening a modal before fully hiding the previous one c... — committed to tomasz1986/syncthing by tomasz1986 10 months ago
- gui: Fix body padding infinitely increasing due to overlapping modals (ref #9063) Opening and hiding multiple modals at the same time as well as opening a modal before fully hiding the previous one c... — committed to tomasz1986/syncthing by tomasz1986 10 months ago
- gui: Fix body padding infinitely increasing due to overlapping modals (ref #9063) (#9078) Opening and hiding multiple modals at the same time as well as opening a modal before fully hiding the previ... — committed to syncthing/syncthing by tomasz1986 9 months ago
- Squashed commit of the following (actually merge upstream/main): commit a405c21ebba3e395a221d91e6654a269e83cdc02 Author: Jakob Borg <jakob@kastelo.net> Date: Sat Oct 14 12:29:53 2023 +0200 cmd... — committed to maxi0604/syncthing by maxi0604 9 months ago
Have you tried using
Hi, I have tried this as follows.
$(“.btn-plus”).click(function(){ $(‘#new_passenger’).modal(“show”).on(‘hide’, function() { $(‘#new_passenger’).modal(‘hide’) }); });
I worked. sorry for my english… =)
I was just searching for this myself. Ended up with the following solution:
The hideModal function sets a flag that it is currently hiding a modal. When it is done it resets the flag. If a modal tries to open while closing is in progress, the id is saved and when closing is complete it shows the modal. This ensures that as long as you call “hideModal” before “showModal”, the modal is closed before the next one is opened.
As a workaround you can remove “fade” class from your modal. If you need to prevent user to close the modal on click you need to add data-backdrop=“static” attribute to your modal.
The issue is that .modal() is async, so those three calls will be fired one after another without waiting the other one to finish, thus causing issues.
Even using toggle causes issue in this scenario. The most sane thing would be to patch the modal proto to add a callback.
$('#show_modal').click(function () { $('#test-modal').modal('show'); $('#test-modal').on('shown.bs.modal', function () { $('#test-modal').modal('hide'); $('#test-modal').on('hidden.bs.modal', function () { $('#test-modal').modal('show'); }); }); });https://getbootstrap.com/docs/3.4/javascript/#modals-methods
My solution with the same problem was put a settimeout in the next ‘hide’ and ‘show’: $(‘#show_modal’).click(function(){ $(‘#test-modal’).modal(‘show’) setTimeout(function(){$(‘#test-modal’).modal(‘hide’)}, 10) setTimeout(function(){$(‘#test-modal’).modal(‘show’)}, 900) }) I know that this isn’t a good solution but resolve my problem. Excuse my English I’m learning…
The below statements show how to open/reopen Modal without using bootstrap.
Add two classes in css
And then use the below jQuery to reopen the modal if it is closed.
$(“#Modal”).removeClass(‘hide_block’); $(“#Modal”).addClass(‘display_block’); $(“Modal”).show(“slow”);
It worked fine for me 😃
Solution about every bug like this one is three steps:
1 - Use bootstrap-modal.js 2 - Set max backdrop like this: $.fn.modalmanager.defaults.backdropLimit = 1; 3 - Use this modal header (check stackable topic): http://jschr.github.io/bootstrap-modal/ 4 - Don Use Button tag in order to open modals, use Links (check stackable topic): http://jschr.github.io/bootstrap-modal/
That tips solves that and more bugs about bootstrap modals
Sorry my english and hope it helps
Based on this answer I’ve made it with this code:
$(modal_selector).on('hidden.bs.modal', function(e){ $(modal_selector).data('bs.modal', null); });It looks that everytime the modal is hidden, it must to be reinitialized, so, you catch
"hidden.bs.modal"and reinitialize the modal with.data('bs.modal', null);It worked fine for me 😃
I had to tweak @kimsy’s version in case a show gets called before the shown is complete. Here is a fiddle version with it working .
I created a Typescript version as well that handles not showing a Modal if the hide is called before the show is ever triggered
Hi guys, in my case, I removed class=“fade” of my modal, and worked! I don’t understood what, but worked.
before:
<div class="modal fade vertical-center col-md-12 col-md-offset-5" id="loading" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> ... </div>AFTER:
<div class="modal vertical-center col-md-12 col-md-offset-5" id="loading" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">...</div>then, I used:
$('#loading').modal('toggle')