You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
modal('show') after a modal('hide') doesn't work #3902
I haven't tried that but that is also not possible generally because hide may be called from one function and show will be called from another function e.g. If I have a single modal dialog for displaying error, I got an error and displayed a msg with a button to take action, action button hides modal calls another function which has again some error and it shows the modal which doesn't come up as mentioned above
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.
Reacted by Burak, Amit Tomar, sunsande, Eren , Kaiwen Bian and Scott Jodoin
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...
Reacted by svsoftwareai
Reacted by MARIE Adeline, Alexis Espinoza Silloca and Bartosz Kruczek
As eskimoblood indicates, this is still an issue with 2.3.1. Using a test for if the modal is visible and if so using a on "hidden" should provide a reasonable work around.
Ugh...
My workaround was to do a $('body') and see if it has the class 'modal-open'. If not -> open the modal. If yes, subscribe the .one('hidden.bs.modal', function(){ open the modal}) and call close() first
I used the $timeout service as I'm using angular. It works most of the times, but the only problem is choosing a reasonable timeout value, since it can vary a lot with browsers/ environments.
I was just searching for this myself. Ended up with the following solution:
var hideInProgress = false;
var showModalId = '';
function showModal(elementId) {
if (hideInProgress) {
showModalId = elementId;
} else {
$("#" + elementId).modal("show");
}
};
function hideModal(elementId) {
hideInProgress = true;
$("#" + elementId).on('hidden.bs.modal', hideCompleted);
$("#" + elementId).modal("hide");
function hideCompleted() {
hideInProgress = false;
if (showModalId) {
showModal(showModalId);
}
showModalId = '';
$("#" + elementId).off('hidden.bs.modal');
}
};
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.
Reacted by Harry Gulliford, Molly Million, Ian Kris Espinoza, Matthew Mitchell, Panu Boonpromsook, Maikel Guerra Ferrer and pisithlykhmer
Reacted by Ian Kris Espinoza, Undeath', Maikel Guerra Ferrer and pisithlykhmer
I created a Typescript version as well that handles not showing a Modal if the hide is called before the show is ever triggered
export module BootstrapExtension{let_actionInProgress=false;let_nextActionQueue=[]asActionQueueInstance[];exporttypeModalAction="hide"|"show";exportinterfaceModalOptions{backdrop?: boolean|"static";keyboard?: boolean;focus?: boolean;show?: boolean;}interfaceActionQueueInstance{modal: JQuery;options: ModalOptions|ModalAction;}/** * Lot's of fun issues arise when attempting to display a modal more than once, before it finishes loading * https://github.com/twbs/bootstrap/issues/3902 * http://jsfiddle.net/daryllabar/npn4wvjm/ * @param modal Id of the modal, or JQuery object of the modal * @param options */exportfunctionsafeModal(modalOrId: string|JQuery,options?: ModalOptions|ModalAction): JQuery{if(!modalOrId){returnmodalOrIdasJQuery;}constmodal=typeofmodalOrId==="string" ? $(`#${modalOrId}`) : modalOrId;constaction=getAction(options);if(_actionInProgress){if(action==="hide"){for(leti=0;i<_nextActionQueue.length;i++){constfutureAction=_nextActionQueue[i];if(futureAction.modal===modal&&getAction(futureAction.options)==="show"){// hide called when show is waiting to execute, clear both_nextActionQueue.splice(i,1);returnmodal;}}}_nextActionQueue.push({modal: modal,options: options});returnmodal;}constpostEventName=getPostEventName(action);_actionInProgress=true;modal.on(postEventName,clearInProgress);modal.modal(options);functionclearInProgress(){_actionInProgress=false;modal.off(postEventName,clearInProgress);if(_nextActionQueue.length>0){constnext=_nextActionQueue.shift();safeModal(next.modal,next.options);}}returnmodal;};functiongetAction(options: ModalOptions|ModalAction): ModalAction{if(isModalAction(options)){returnoptions;}elseif(!options){return"show";}else{returnoptions.show ? "show" : "hide";}}functiongetPostEventName(action: ModalAction): string{constprefix=action==="hide" ? "hidden" : "shown";returnprefix+".bs.modal";}exportfunctionisModalAction(arg: any): arg is ModalAction{switch(arg){case"hide":
case"show":
case"toggle":
returntrue;default:
returnfalse;}}}
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.
Reacted by Ege Tuncoz, Jonatan von Martens, jmm13, Alireza Sadeghi, murakamibruno and Bang Andre
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 :)
Reacted by Toriq Setiawan and Matías Nehuen Malpartida
Execution of the .modal() method takes time. If during execution the next modal() is called, it has no effect (see documentation). For a please wait modal I created following solution
var showPleaseWait=0;
function pleaseWaitInitialize()
{
$('#pleaseWaitDialog').on('shown.bs.modal', function (e) {
if (!showPleaseWait)
{
$("#pleaseWaitDialog").modal("hide");
}
});
}
function pleaseWaitShow()
{
showPleaseWait=1;
$("#pleaseWaitDialog").modal("show");
}
function pleaseWaitHide()
{
showPleaseWait=0;
$("#pleaseWaitDialog").modal("hide");
}
In this way whichever if the modal("hide") in pleaseWaitHide() has no effect because .modal("show") isn't ready, the 2nd modal("hide") in the ready call back is taking effect.
Of course this concept can be extended to your problem
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...
sometimes still not working, excuse for my poor english...
If you are using the Bootstrap JavaScript library to handle modals, calling modal('show') immediately after modal('hide') may not work as expected. This is because the modal('hide') function triggers an event to hide the modal, which takes some time to complete. If you immediately call modal('show') right after modal('hide'), the show event may be triggered before the hide event is finished, resulting in unexpected behavior.
To ensure that the modal is shown after it has been fully hidden, you can use the modal's events and callbacks.
$('#myModal').on('hidden.bs.modal', function () {
$('#myModal').modal('show');
});
By using the hidden.bs.modal event and its corresponding callback, you can ensure that the bootstrap modal is properly hidden before attempting to show it again, avoiding any conflicts or unexpected behavior.
Activity
EricWarnke commentedon Jun 22, 2012
Have you tried using
anuraguniyal commentedon Jun 22, 2012
I haven't tried that but that is also not possible generally because hide may be called from one function and show will be called from another function e.g. If I have a single modal dialog for displaying error, I got an error and displayed a msg with a button to take action, action button
hidesmodal calls another function which has again some error and itshowsthe modal which doesn't come up as mentioned abovesaibayadon commentedon Jun 22, 2012
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.
canteroalejandro commentedon Oct 15, 2012
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.. =)
fat commentedon Feb 8, 2013
was a bug with that – fixed in 2.3
eskimoblood commentedon Apr 4, 2013
Still there in 2.3.1 : http://jsfiddle.net/ngegC/
Lutxi commentedon May 12, 2013
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...
WesleyKapow commentedon May 20, 2013
As eskimoblood indicates, this is still an issue with 2.3.1. Using a test for if the modal is visible and if so using a on "hidden" should provide a reasonable work around.
jimbuho commentedon Jul 8, 2013
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
janvanderhaegen commentedon May 12, 2015
Ugh...
My workaround was to do a $('body') and see if it has the class 'modal-open'. If not -> open the modal. If yes, subscribe the .one('hidden.bs.modal', function(){ open the modal}) and call close() first
narainmittal commentedon Dec 23, 2015
I used the $timeout service as I'm using angular. It works most of the times, but the only problem is choosing a reasonable timeout value, since it can vary a lot with browsers/ environments.
kimsy commentedon Mar 3, 2016
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.
daryllabar commentedon Nov 3, 2016
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
gcishevs commentedon Dec 28, 2017
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.
kirankumardm141 commentedon Jun 20, 2018
The below statements show how to open/reopen Modal without using bootstrap.
Add two classes in css
display:none !important;
}
display:block !important;
}
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 :)
alexsazo commentedon May 14, 2019
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 :)
scubajorgen commentedon Apr 9, 2020
Execution of the .modal() method takes time. If during execution the next modal() is called, it has no effect (see documentation). For a please wait modal I created following solution
In this way whichever if the modal("hide") in pleaseWaitHide() has no effect because .modal("show") isn't ready, the 2nd modal("hide") in the ready call back is taking effect.
Of course this concept can be extended to your problem
lukeracortes commentedon Sep 22, 2020
$('#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
HigorSenna commentedon Jan 11, 2021
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')bernie-ning commentedon May 7, 2021
sometimes still not working, excuse for my poor english...
Adebisi01 commentedon Feb 6, 2023
$("#firstModal").modal('show');
$('#firstModal').on('hidden.bs.modal', function (e) {
$("#secondModal").modal('show');
})
$("#firstModal").modal('hide');
This worked for me for boostrap 5
carlosridg commentedon May 15, 2023
If you are using the Bootstrap JavaScript library to handle modals, calling modal('show') immediately after modal('hide') may not work as expected. This is because the modal('hide') function triggers an event to hide the modal, which takes some time to complete. If you immediately call modal('show') right after modal('hide'), the show event may be triggered before the hide event is finished, resulting in unexpected behavior.
To ensure that the modal is shown after it has been fully hidden, you can use the modal's events and callbacks.
By using the hidden.bs.modal event and its corresponding callback, you can ensure that the bootstrap modal is properly hidden before attempting to show it again, avoiding any conflicts or unexpected behavior.
gui: Fix modals overlapping and body padding added when saving config…
gui: Fix body padding infinitely increasing due to overlapping modals…
gui: Fix body padding infinitely increasing due to overlapping modals…
gui: Fix body padding infinitely increasing due to overlapping modals…
gui: Fix body padding infinitely increasing due to overlapping modals…
Squashed commit of the following (actually merge upstream/main):