Adam Carr
Adam Carr

Reputation: 2986

Last jQuery modal dialog z-index overrides initial modal z-index

I have a need to show 2 dialog modals at once. Due to the contents of the first dialog needing to use some absolute positioning and z-indexing, the z-index of the overlay is important to me.

The problem I get is if I show a the first modal at z-index of 300, the overlay gets a z-index of 301. If I then show another modal with a z-index of 500, the new overlay gets a z-index of 501. If I close both of the modals and open the first modal again, instead of getting an overlay with z-index of 301, it is 503.

Here is some sample code.

<html>                                                                  
<head>                                                                  
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.13.custom.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js" type="text/javascript"></script>        
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.js" type="text/javascript"></script>        
<script type="text/javascript">              
$(document).ready(function(){
    $('#modal').hide();
    $('#success-message').hide();
    $('#show-modal-button').click(function(){
        $('#modal').dialog({
              modal: true,
              buttons: {
                  OK: function () {
                      $(this).dialog("close");
                  }
              },
              draggable: false,
              title: 'test modal',
              resizable: false,
              zIndex: 400
          });

    });

    $('#modal-button').click(function(){
        $('#success-message').dialog({
              modal: true,
              buttons: {
                  OK: function () {
                      $(this).dialog("close");
                  }
              },
              draggable: false,
              title: 'test modal',
              resizable: false,
              zIndex: 500
          });
    });
});
</script>                                                               
</head>                                                                 
<body>     
<input type="button" id="show-modal-button" value="show modal"/>      
<div id="modal">
    <input type="button" id="modal-button" value="push"/>
</div>                       
<div id="success-message"><p>test</p></div>                
</body>                                                                 
</html>

UPDATE I was able to get this to work by removing the widget from the DOM when closing using the code below. I feel like this is a hack and that it is either a bug or that I am doing something wrong in my approach. I'll post my solution as an answer if no one can tell me what I am doing wrong.

$('#modal-button').click(function(){        
    $('#success-message').dialog({
        modal: true,
        buttons: {
            OK: function () {
                $(this).dialog("close");
                $(this).dialog('widget').remove();
            }
        },
        draggable: false,
        title: 'test modal',
        resizable: false,
        zIndex: 500
    });     
});

Upvotes: 3

Views: 32527

Answers (3)

chris86brown
chris86brown

Reputation: 41

'stack: false' worked for me.

It seems setting it false stops the dialog recalculating its z-index when it is opened, or clicked or whatever.

Upvotes: 0

narhe
narhe

Reputation: 51

Try setting the "stack" option to false:

'stack: false'

That might work for you

Upvotes: 5

Adam Carr
Adam Carr

Reputation: 2986

I was able to get this to work by removing the widget from the DOM when closing using the code below. I feel like this is a hack and that it is either a bug or that I am doing something wrong in my approach. I'll post my solution as an answer if no one can tell me what I am doing wrong.

$('#modal-button').click(function(){        
    $('#success-message').dialog({
        modal: true,
        buttons: {
            OK: function () {
                $(this).dialog("close");
                $(this).dialog('widget').remove();
            }
        },
        draggable: false,
        title: 'test modal',
        resizable: false,
        zIndex: 500
    });     
});

Upvotes: 9

Related Questions