1

I am new to jQuery. I am creating an asp.net MVC application.

I am displaying a dialog when user clicks on a button using the following code.

  $(document).ready(function () {
    $dialog = $("#quantityDialog").dialog({
      autoOpen: false,
      title: 'Add to Cart',
    });
    $('.AddToCart').on('click', function () {
      $dialog.dialog('open');
    });
  });

HTML:

<div id="quantityDialog" style="display:none">
...
</div>

The dialog is working fine.

  1. I need to restrict the user from being able to click other elements of the page when the dialog is being displayed.
  2. Also when the user clicks elsewhere (outside the dialog box) the dialog has to be closed.

How can I achieve this. Help me out.

3
  • You need an overlay element to go under the dialog. There is an option to jQueryUI Dialog to do that already see jqueryui.com/dialog/#modal-confirmation Commented Jul 15, 2014 at 8:52
  • 1
    You can use colorbox jacklmoore.com/colorbox to show dialog it will give you more functionality like iframe inline etc to open dialog Commented Jul 15, 2014 at 8:53
  • there is an option for that: modal: true Commented Jul 15, 2014 at 9:01

4 Answers 4

1
 $("#loadPreviewDiv").click(function () {
        $('#dialog').dialog({
            title: "Dialog box",
            height: 300,
            modal: true,
            open: function() {
            $('.ui-widget-overlay').on('click', function() {
                $('#dialog').dialog('close');
            })
        },
            buttons: { "close": function() { $(this).dialog("close"); } } 
        });
 });

Demo:

http://jsfiddle.net/2hyAh/1/

Sign up to request clarification or add additional context in comments.

1 Comment

your solution works but when I click outside the box the dialog is not getting closed!
1

Here is your solution,

  1. Diable click of other area

    Here you can add one dom to overlap the other area

    <div class="overlay"></div>
    

    And write the css for this dom

    .overlay {
        display: none;
        position: fixed;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        opacity: 0;
        z-index: 1;
    }
    

    When you open the popup, you can show this overlay by simply writing this.

    $('.overlay').show();    
    

    Note : Make sure that popup z-index is greater than the overlay.

  2. Close the popup on click outside of it

    $('body').click(function(e){
        if(!$(e.target).is('.AddToCart, #quantityDialog'))
        {
            // write logic to close the popup here
            $('.overlay').hide();
        }
    })
    

Comments

1

DEMO

JS code:

$(document).ready(function () {
    $dialog = $("#quantityDialog").dialog({
      autoOpen: false,
      modal:true,//to display an overlay
      title: 'Add to Cart',
    });

$('.AddToCart').on('click', function () {
  $dialog.dialog('open');
});



/*When clicked on overlay close the dailog*/
   $('div.ui-widget-overlay').click(function(){
       $dialog.dialog('close');
   });
  });

Comments

0

You can use modal property of dialog box to achieve the background overlay ,

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

And to close on overlay you can bind the .ui-widget-overlay in open of dialog box as ::

$("#dialog").dialog({
        ......
        open: function(){
            $('.ui-widget-overlay').on('click',function(){
                $('#dialog').dialog('close');
            });
        ....
        }

1 Comment

solution works but when I click outside the box the dialog is not getting closed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.