So, I'm using this code to open another modal window in a current opened modal window:
<a onclick=\"$('#login').modal('hide');$('#lost').modal('show');\" href='#'>Click</a>
What happens is, that for like 500ms the scrollbar will duplicate. I guess because the current modal is still fading out. However it looks very un-smooth and stuttering.
I would appreciate any suggestions to solve this issue.
Also, is the way building this in an onclick-event unprofessional?
I'm working with the bootstrap version 3.0.
Edit: I guess it's neccesary to reduce the time of fading out a modal. How is this possible?
I went kind of a different route all together, I decided to "De-Nest" them. Maybe someone will find this handy...
data-dismiss
makes the current modal window force closedata-toggle
opens up a new modal with thehref
content inside itor
do let us know if it works.
Why not just change the content of the modal body?
In the modal just put a link or a button
If you just want to switch beetween 2 modals:
In the modal just put a link or a button
You can actually detect when the old modal closes by calling the
hidden.bs.modal
event:For more info: http://getbootstrap.com/javascript/#modals-events
Update for 2018 -- Using Bootstrap 4
I found most of the answers seem to have a lot of unnecessary jQuery. To open a modal from another modal can be done simply by using the events that Bootstrap provides such as "show.bs.modal". You may also want some CSS to handle the backdrop overlays. Here are the 3 open modal from another scenarios...
Open modal from another modal (keep 1st modal open)
A potential issue in this case is that the backdrop from the 2nd modal hide the 1st modal. To prevent this, make the 2nd modal
data-backdrop="static"
, and add some CSS to fix the z-indexes of the backdrops..https://www.codeply.com/go/NiFzSCukVl
Open modal from another modal (close 1st modal)
This is similar to the above scenario, but since we are closing the 1st modal when the 2nd is opened, there is no need for the backdrop CSS fix. A simple function that handles the 2nd modals
show.bs.modal
event closes the 1st modal.$("#myModal2").on('show.bs.modal', function (e) { $("#myModal1").modal("hide"); });
https://www.codeply.com/go/ejaUJ4YANz
Open modal inside another modal
The last multiple modals scenario is opening the 2nd modal inside the 1st modal. In this case the markup for the 2nd is placed inside the 1st. No extra CSS or jQuery is needed.
https://www.codeply.com/go/iSbjqubiyn
My solution does not close the lower modal, but truly stacks on top of it. It preserves scrolling behavior correctly. Tested in Bootstrap 3. For modals to stack as expected, you need to have them ordered in your Html markup from lowest to highest.
UPDATE: When you have stacked modals, all the backdrops appear below the lowermost modal. You can fix that by adding the following CSS:
This will give the appearance of a modal backdrop below the topmost visible modal. That way it grays out your lower modals underneath.