Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI dialogs: how to close dialog when click outside?

In docs I didn't see such information.

There are options to close dialog in such cases:

1) push Esc;

2) click on "OK" or "Close" buttons in the dialog.

But how to close dialog if click outside?

Thanks!

like image 564
Vitalii Ponomar Avatar asked Nov 29 '11 06:11

Vitalii Ponomar


2 Answers

Here are 2 other solutions for non-modal dialogs:

If dialog is non-modal Method 1: method 1: http://jsfiddle.net/jasonday/xpkFf/

// Close Pop-in If the user clicks anywhere else on the page
                     jQuery('body')
                      .bind(
                       'click',
                       function(e){
                        if(
                         jQuery('#dialog').dialog('isOpen')
                         && !jQuery(e.target).is('.ui-dialog, a')
                         && !jQuery(e.target).closest('.ui-dialog').length
                        ){
                         jQuery('#dialog').dialog('close');
                        }
                       }
                      );

Non-Modal dialog Method 2: http://jsfiddle.net/jasonday/eccKr/

  $(function() {
            $( "#dialog" ).dialog({
                autoOpen: false, 
                minHeight: 100,
                width: 342,
                draggable: true,
                resizable: false,
                modal: false,
                closeText: 'Close',
                  open: function() {
                      closedialog = 1;
                      $(document).bind('click', overlayclickclose);
                  },
                  focus: function() {
                      closedialog = 0;
                  },
                  close: function() {
                      $(document).unbind('click');
                  }



        });

         $('#linkID').click(function() {
            $('#dialog').dialog('open');
            closedialog = 0;
        });

         var closedialog;

          function overlayclickclose() {
              if (closedialog) {
                  $('#dialog').dialog('close');
              }

              //set to one because click on dialog box sets to zero
              closedialog = 1;
          }


  });
like image 104
Jason Avatar answered Oct 30 '22 10:10

Jason


I found solution on ryanjeffords.com:

<script type="text/javascript">

    $(document).ready(function() {

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

        $('.ui-widget-overlay').live("click",function(){
            $("#dialog").dialog("close");
        });    
    });   
</script>
like image 31
Vitalii Ponomar Avatar answered Oct 30 '22 09:10

Vitalii Ponomar