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

5

7

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!

Leucite answered 29/11, 2011 at 6:0 Comment(3)
In terms of usability, it is in my opinion a strange behavior to close a dialog when you click outside if the dialog is not modal. Anyway, here's a solution.Tango
May be [this][1] or [this][2] will help you. [1]: #8302614 [2]: #7919729Mountford
possible duplicate of jQuery UI - Close Dialog When Clicked OutsideCounter
H
7

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;
          }


  });
Hibiscus answered 1/2, 2012 at 18:31 Comment(0)
L
7

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>
Leucite answered 29/11, 2011 at 6:37 Comment(1)
Too bad... I'll try to find other solutionLeucite
H
7

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;
          }


  });
Hibiscus answered 1/2, 2012 at 18:31 Comment(0)
B
5

If dialog is modal, then paste these 3 lines of code in the open function when you create your dialog options:

open: function(event,ui) {
    $('.ui-widget-overlay').bind('click', function(event,ui) {         
        $('#myModal').dialog('close');
    });
}
Bellringer answered 12/6, 2014 at 9:11 Comment(0)
D
1

Facing the same problem, I have created a small plugin that enables to close a dialog when clicking outside of it whether it a modal or non-modal dialog. It supports one or multiple dialogs on the same page.

More information on my website here: http://www.coheractio.com/blog/closing-jquery-ui-dialog-widget-when-clicking-outside

The plugin is also on github: https://github.com/coheractio/jQuery-UI-Dialog-ClickOutside

Laurent

Diarmit answered 9/9, 2013 at 13:0 Comment(0)
S
0

This is my solution.

I have, for example

<div id="dialog1">Some content in here</div>
<div id="dialog2">Different content in here</div>
<div id="dialog3">And so on...</div>

Each div gets opened as a dialog depending on what the user interacts with. So being able to close the currently active one, I do this.

// This closes the dialog when the user clicks outside of it.
$("body").on('click', '.ui-widget-overlay', function() {
    if( $("div.ui-dialog").is(":visible") )
    {
        var openDialogId = $(".ui-dialog").find(".ui-dialog-content:visible").attr("id");
        if ($("#"+openDialogId).dialog("isOpen"))
        {
            $("#"+openDialogId).dialog('close');
        }
    }        
});
Shall answered 16/12, 2013 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.