Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery UI modal dialog global close on overlay click
    primarykey
    data
    text
    <p>I have several modal dialogs in my page and there will be even more. I plan to use modal dialogs for them all and have them close on overlay click. I don't want to bind the overlay click event in every place I instantiate a dialog, so I would like to extend the dialog prototype or something similar to make it bind the click event globally for all modal dialogs.</p> <p><a href="http://jsfiddle.net/jurchiks/kLBJm/1/" rel="nofollow">http://jsfiddle.net/jurchiks/kLBJm/1/</a></p> <p>Let's say I have the following dialog constructor:</p> <pre><code>$('#dialog').dialog({ modal: true, open: function() { $(this).find('input[type=text]').focus(); } }); </code></pre> <p>I've tried putting this code before dialog instantiation:</p> <pre><code>$.extend( $.ui.dialog.prototype.options, { open: function() { var dialog = this; $('.ui-widget-overlay').on('click', function() { $(dialog).dialog('close'); }); } } ); </code></pre> <p>but it does not work, jQuery only calls the open function passed in the dialog parameters. I have suspicion that this simply changes the default open function and whatever is passed to the constructor is overriding this.</p> <pre><code>$.widget( "ui.dialog", $.ui.dialog, { open: function() { this._super(); var dialog = this; $('.ui-widget-overlay').on('click', function() { $(dialog).dialog('close'); }); } } ); </code></pre> <p>This does not work either; jQuery spits out an error - "cannot call methods on dialog prior to initialization; attempted to call method 'close'" - even though the popup is open. How could I make it so the overlay click &amp; close event is global and not overridable?</p> <p>P.S. why the hell is this not provided by the jQuery UI library? I would think this is something very popular.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload