26

Here's my scenario: I'm opening a modal window with some record details, and I've a "Delete" button. When user clicks on this button, I need to show a "confirmation" modal above/over the existing modal (asking "are you sure?"), but when this confirmation modal is showed, it doesn't block the "details" first modal (behind).

Does anyone know how can I do it?

Thanks!

Andres I Perez
  • 75,075
  • 21
  • 157
  • 138
Rodrigo Pires
  • 574
  • 2
  • 11
  • 23
  • 3
    It would seem that there isn't much in the way of an actual answer, but this extension to the modal class might be the way to go: https://github.com/jschr/bootstrap-modal – Joel B Jun 24 '13 at 22:38
  • A working example can be found at http://bootply.com/lvKQA2AM28 – CrandellWS Jun 12 '14 at 04:18
  • I replied this same problem here: http://stackoverflow.com/questions/19528173/bootstrap-open-another-modal-in-modal/32513228 – Elon Gomes Vieira Sep 10 '15 at 23:38

5 Answers5

19

All you need to do is place the markup for the confirmation modal lower down in the code than the details modal.

Rob
  • 406
  • 3
  • 12
  • You're a genius. I looked for this for almost a day. – Chris Sharp Jun 05 '18 at 12:37
  • try this with a very long first modal. you will see that after closing the second modal, the background scrolls instead of the foreground. the docs clearly state "Multiple open modals not supported. [This] requires custom code." – interDist Sep 05 '18 at 05:34
  • interDist - I agree, after finding this out myself. I now only show one modal at a time in my application – Rob Oct 06 '18 at 09:38
  • This solution solved my problem - which was that the buttons of the "Confirmation" modal (in my case - "click here otherwise you will be automatically logged out") were not working. – gordon613 Jan 10 '19 at 11:28
  • @interDist : You can check a workaround here : https://github.com/twbs/bootstrap/issues/20607#issuecomment-247690824 . basically adding the modal-open class to the body on closing the second modal. – Monster Brain Mar 01 '19 at 06:18
  • Does not work... – TV-C-1-5 Mar 12 '22 at 02:05
16

It's quite easy to do it. Links in your already opened modal have to look like this:

<a href="NEW URL" data-dismiss="modal" data-toggle="modal" data-target="#newModal">Anchor text</a>

data-dismiss="modal" -> will close that modal = that is the trick!!!!
data-toggle="modal" -> will open new dialog

Enjoy!

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Oleg Veselkov
  • 185
  • 1
  • 3
6

Simply hide #modal and show #modal2 when click on #deleteButton

$("#deleteButton").click(function() {
$('#myModal').modal('hide')
$('#myModal2').modal('show')    
});

Here's a working example: http://jsfiddle.net/baptme/nuUzN/14/

baptme
  • 10,062
  • 3
  • 52
  • 57
  • The scrolling wouldn't work on the second modal if the height is long. – Giraldi May 15 '15 at 02:52
  • You should detect when the old modal closes first before executing the new modal... Here was my solution: http://stackoverflow.com/a/30250853/851045 – Giraldi May 15 '15 at 03:06
  • The question asks how to have one modal over the other - not how to close one before opening another. – TV-C-1-5 Mar 12 '22 at 02:06
5

How about adding a z-index to the second modal? Like:

<div class="modal fade" style="z-index:1051">modal content here...</div>

If the value 1051 doesn't work try a higher number, eg. 1061 for Bootstrap 5.

Meilo
  • 3,378
  • 3
  • 28
  • 34
fedda
  • 216
  • 4
  • 7
0

If you want to keep the first modal in the back, you have to do a couple of things. Note that it is not supported by bootstrap (using bootstrap 4.5).

As @fedda said, you need to specify a z-index on the modal element for it to be over the first one (1050 is the default) and then you have to also set the z-index of the backdrop (everything is darken in the back of the modal) to be over the first one and finally, if your first modal can be scrollable (big modal or small screen), you have to reconfigure the scroll to the first modal when the second one closes.

<div id="modal2" class="modal fade" style="z-index:1055">modal content here...</div>

Then in the script:

$("#modal2").on("hidden.bs.modal", () => 
    {$("#modalintheback").data("bs.modal")._setScrollbar();}
$("#modal2").modal();
$("#modal2").next(".modal-backdrop").attr("style", "z-index:1054;");
Yepeekai
  • 2,545
  • 29
  • 22
  • while this apparently works, I got a js console error from bootstrap if I show two modals at the same time `Maximum call stack size exceeded` – raquelhortab Jan 12 '23 at 10:10