Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI dialog box manual close

Tags:

jquery

popup

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.

like image 277
Sree ram Avatar asked Jan 13 '23 09:01

Sree ram


2 Answers

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/

like image 119
Samich Avatar answered Jan 14 '23 21:01

Samich


Register your click event this way:

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

Working Fiddle

like image 29
Greenhorn Avatar answered Jan 14 '23 23:01

Greenhorn