Vitalii Ponomar
Vitalii Ponomar

Reputation: 10936

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!

Upvotes: 7

Views: 16523

Answers (5)

Oskar
Oskar

Reputation: 2562

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

Upvotes: 5

Justin
Justin

Reputation: 11

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

Upvotes: 0

Laurent
Laurent

Reputation: 430

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

Upvotes: 1

Jason
Jason

Reputation: 7682

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


  });

Upvotes: 7

Vitalii Ponomar
Vitalii Ponomar

Reputation: 10936

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>

Upvotes: 7

Related Questions