Sree ram
Sree ram

Reputation: 379

jQuery UI dialog box manual close

I am using jQuery UI Dialog Box.

When I am clicking on a button the dialog box should open. When the dialog box is opened then whole body should be in a disable state. Like how exactly when we are using a popup. So for that I have used the below code.

Here is Js Fiddle Link

<!doctype html>

<html lang="en">
<head>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(document).ready(function(e) {
    $("#popup").click(function(){
        $( "#dialog" ).dialog();
        $( ".parentDisable" ).show();
    });

    $(".parentDisable").click(function(){
        $( "#dialog" ).dialog('close');
        $( ".parentDisable" ).fadeOut(1000);
    });

    $(".ui-button-icon-primary").click(function(){
        $( "#dialog" ).dialog('close');
        $( ".parentDisable" ).fadeOut(1000);
    });

});
</script>
<style>
    .parentDisable{
    position:fixed;
    top:0;
    left:0;
    background:#000;
    opacity:0.8;
    z-index:1;
    height:100%;
    width:100%;}
</style>
</head>
<body>

<div id="dialog" title="Basic dialog" style="display:none;">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

<div class="parentDisable" style="display:none;"></div>


<span id="popup" style="color:blue;cursor:pointer">Pop Up</span>


</body>
</html>

Here I have problem. When I am clicking on button pop up is opening and the whole body is covered with Black background.

Now the user should able to close in two types.

  1. By clicking on close symbol in the popup.
  2. By clicking on out side area of popup (On Black Background)

The 2nd way mentioned above is working fine. But in the first method when I am clicking on close symbol only the POPUP is getting close and the black background is remains same.

I have tried in some ways. But it has not worked.
Please give any suggestions.

Upvotes: 4

Views: 1474

Answers (4)

Greenhorn
Greenhorn

Reputation: 1700

Register your click event this way:

 $(document).on('click','.ui-button-icon-primary',function(){
        $( "#dialog" ).dialog('close');
        $( ".parentDisable" ).fadeOut(1000);
    });

Working Fiddle

Upvotes: 2

Samich
Samich

Reputation: 30115

You can subscribe to the close event of the dialog and hide your background:

$( "#dialog" ).on( "dialogclose", function( event, ui ) { 
    $( ".parentDisable" ).fadeOut(1000); 
});

http://jsfiddle.net/nRQXA/3/

update

Such functionality already exists in the dialog component:

  $( "#dialog" ).dialog(
    { 
       modal: true 
  });

http://jsfiddle.net/nRQXA/23/

Upvotes: 6

Chirag Vidani
Chirag Vidani

Reputation: 2587

Register dialog with close event

$("#dialog").dialog({
         autoOpen: false,
         close: function (event, ui) {
             $(".parentDisable").fadeOut(1000);
         }
     });

Open it with open command

 $("#dialog").dialog('open');

Check out updated fiddle

Upvotes: 1

Shashank
Shashank

Reputation: 829

  $("#dialog").dialog({
            buttons: { "Ok": function() {  $(this).dialog("close");}}
            });

Upvotes: 0

Related Questions