JqueryUI - Dialog


Advertisements


Dialog boxes are one of the nice ways of presenting information on an HTML page. A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default.

jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.

Syntax

The dialog() method can be used in two forms −

$ (selector, context).dialog (options) Method

The dialog (options) method declares that an HTML element can be administered in the form of a dialog box. The options parameter is an object that specifies the appearance and behavior of that window.

Syntax

$(selector, context).dialog(options);

You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).dialog({option1: value1, option2: value2..... });

The following table lists the different options that can be used with this method −

Sr.No. Option & Description
1 appendTo

If this option is set to false, it will prevent the ui-draggable class from being added in the list of selected DOM elements. By default its value is true.

Option - appendTo

This option is used to append dialog box to the specified element. By default its value is "body".

Syntax

$(".selector").dialog(
   { appendTo: "#identifier" }
);
2 autoOpen

This option unless set to false, the dialog box is opened upon creation. When false, the dialog box will be opened upon a call to dialog('open'). By default its value is true.

Option - autoOpen

This option unless set to false, the dialog box is opened upon creation. When false, the dialog box will be opened upon a call to dialog('open'). By default its value is true.

Syntax

$(".selector").dialog(
   { autoOpen: false }
);
3 buttons

This option adds buttons in the dialog box. These are listed as objects, and each property is the text on the button. The value is a callback function called when the user clicks the button. By default its value is {}.

Option - buttons

This option adds buttons in the dialog box. These are listed as objects, and each property is the text on the button. The value is a callback function called when the user clicks the button. By default its value is {}.

This handler is invoked with a function context of the dialog box element, and is passed the event instance with the button set as the target property. If omitted, no buttons are created for the dialog box.

Syntax

$(".selector").dialog(
   { buttons: [ { text: "Ok", click: function() { $( this ).dialog( "close" ); } } ] }
);
4 closeOnEscape

Unless this option set to false, the dialog box will be closed when the user presses the Escape key while the dialog box has focus. By default its value is true.

Option - closeOnEscape

Unless this option set to false, the dialog box will be closed when the user presses the Escape key while the dialog box has focus. By default its value is true.

Syntax

$(".selector").dialog(
   { closeOnEscape: false }
);
5 closeText

This option contains text to replace the default of Close for the close button. By default its value is "close".

Option - closeText

This option contains text to replace the default of Close for the close button. By default its value is "close".

Syntax

$(".selector").dialog(
   { closeText: "hide" }
);
6 dialogClass

If this option is set to false, it will prevent the ui-draggable class from being added in the list of selected DOM elements. By default its value is "".

Option - dialogClass

If this option is set to false, it will prevent the ui-draggable class from being added in the list of selected DOM elements. By default its value is "".

Syntax

$(".selector").dialog(
   { dialogClass: "alert" }
);
7 draggable

Unless you this option to false, dialog box will be draggable by clicking and dragging the title bar. By default its value is true.

Option - draggable

Unless you this option to false, dialog box will be draggable by clicking and dragging the title bar. By default its value is true.

Syntax

$(".selector").dialog(
   { draggable: false }
);
8 height

This option sets the height of the dialog box. By default its value is "auto".

Option - height

If this option sets the height of the dialog box. By default its value is "auto". This option can be of type −

This can be of type −

  • Number − This specifies duration in milliseconds

  • String − The only supported string value is auto which will allow the dialog height to adjust based on its content.

Syntax

$(".selector").dialog(
   { height: 400 }
);
9 hide

This option is used to set the effect to be used when the dialog box is closed. By default its value is null.

Option - hide

This option is used to set the effect to be used when the dialog box is closed. By default its value is null.

This can be of type −

  • Boolean − Values could be true/false. If false no animation will be used and the dialog will be hidden immediately. If true, the dialog will fade out with the default duration and the default easing.

  • Number − The dialog will fade out with the specified duration and the default easing.

  • String − The dialog will be hidden using the specified effect such as slideUp, fold.

  • Object − If the value is an object, then effect, delay, duration, and easing properties may be provided to hide the dialog.

    10

Syntax

$(".selector").dialog(
   { hide: { effect: "explode", duration: 1000 } }
);
11 maxHeight

This option sets maximum height, in pixels, to which the dialog box can be resized. By default its value is false.

Option - maxHeight

This option sets maximum height, in pixels, to which the dialog box can be resized. By default its value is false.

Syntax

$(".selector").dialog(
   { maxHeight: 600 }
);
12 maxWidth

This option sets the maximum width to which the dialog can be resized, in pixels. By default its value is false.

Option - maxWidth

This option sets the maximum width to which the dialog can be resized, in pixels. By default its value is false.

Syntax

$(".selector").dialog(
   { maxWidth: 600 }
);
13 minHeight

This option is the minimum height, in pixels, to which the dialog box can be resized. By default its value is 150.

Option - minHeight

This option is the minimum height, in pixels, to which the dialog box can be resized. By default its value is 150.

Syntax

$(".selector").dialog(
   { minHeight: 200 }
);
14 minWidth

This option is the minimum width, in pixels, to which the dialog box can be resized. By default its value is 150.

Option - minWidth

This option is the minimum width, in pixels, to which the dialog box can be resized. By default its value is 150.

Syntax

$(".selector").dialog(
   { minWidth: 200 }
);
15 modal

If this option is set to true, the dialog will have modal behavior; other items on the page will be disabled, i.e., cannot be interacted with. Modal dialogs create an overlay below the dialog but above other page elements. By default its value is false.

Option - modal

If this option is set to true, the dialog will have modal behavior; other items on the page will be disabled, i.e., cannot be interacted with. Modal dialogs create an overlay below the dialog but above other page elements. By default its value is false.

Syntax

$(".selector").dialog(
   { modal: true }
);
16 position

This option specifies the initial position of the dialog box. Can be one of the predefined positions: center (the default), left, right, top, or bottom. Can also be a 2-element array with the left and top values (in pixels) as [left,top], or text positions such as ['right','top']. By default its value is { my: "center", at: "center", of: window }.

Option - position

This option specifies the initial position of the dialog box. Can be one of the predefined positions: center (the default), left, right, top, or bottom. Can also be a 2-element array with the left and top values (in pixels) as [left,top], or text positions such as ['right','top']. By default its value is { my: "center", at: "center", of: window }.

Syntax

$(".selector").dialog(
   { position: { my: "left top", at: "left bottom", of: button } }
);
17 resizable

This option unless set to false, the dialog box is resizable in all directions. By default its value is true.

Option - resizable

This option unless set to false, the dialog box is resizable in all directions. By default its value is true.

Syntax

$(".selector").dialog(
   { resizable: false }
);
18 show

This option is an effect to be used when the dialog box is being opened. By default its value is null.

Option - show

This option is an effect to be used when the dialog box is being opened. By default its value is null.

This can be of type −

  • Boolean − Values could be true/false. If false no animation will be used and the dialog will be shonw immediately. If true, the dialog will fade in with the default duration and the default easing.

  • Number − The dialog will fade in with the specified duration and the default easing.

  • String − The dialog will be shown using the specified effect such as slideDown, fold.

  • Object − If the value is an object, then effect, delay, duration, and easing properties may be provided to show the dialog.

    19

Syntax

$(".selector").dialog(
   { show: { effect: "blind", duration: 800 } }
);
20 title

This option specifies the text to appear in the title bar of the dialog box. By default its value is null.

Option - title

This option specifies the text to appear in the title bar of the dialog box. By default its value is null.

Syntax

$(".selector").dialog(
   { title: "Dialog Title" }
);
21 width

This option specifies the width of the dialog box in pixels. By default its value is 300.

Option - width

This option specifies the width of the dialog box in pixels. By default its value is 300.

Syntax

$(".selector").dialog(
   { width: 500 }
);

The following section will show you a few working examples of dialog functionality.

Default functionality

The following example demonstrates a simple example of dialog functionality passing no parameters to the dialog() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#dialog-1" ).dialog({
               autoOpen: false,  
            });
            $( "#opener" ).click(function() {
               $( "#dialog-1" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <div id = "dialog-1" 
         title = "Dialog Title goes here...">This my first jQuery UI Dialog!</div>
      <button id = "opener">Open Dialog</button>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −

Use of buttons, title and position

The following example demonstrates the usage of three options buttons, title and position in the dialog widget of JqueryUI.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#dialog-2" ).dialog({
               autoOpen: false, 
               buttons: {
                  OK: function() {$(this).dialog("close");}
               },
               title: "Success",
               position: {
                  my: "left center",
                  at: "left center"
               }
            });
            $( "#opener-2" ).click(function() {
               $( "#dialog-2" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <div id = "dialog-2"
         title = "Dialog Title goes here...">This my first jQuery UI Dialog!</div>
      <button id = "opener-2">Open Dialog</button>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −

Use of hide, show and height

The following example demonstrates the usage of three options hide, show and height in the dialog widget of JqueryUI.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#dialog-3" ).dialog({
               autoOpen: false, 
               hide: "puff",
               show : "slide",
               height: 200      
            });
            $( "#opener-3" ).click(function() {
               $( "#dialog-3" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <div id = "dialog-3"
         title = "Dialog Title goes here...">This my first jQuery UI Dialog!</div>
      <button id = "opener-3">Open Dialog</button>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −

Use of Modal

The following example demonstrates the usage of three options buttons, title and position in the dialog widget of JqueryUI.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#dialog-4" ).dialog({
               autoOpen: false, 
               modal: true,
               buttons: {
                  OK: function() {$(this).dialog("close");}
               },
            });
            $( "#opener-4" ).click(function() {
               $( "#dialog-4" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <div id = "dialog-4" title = "Dialog Title goes here...">This my first jQuery UI Dialog!</div>
      <button id = "opener-4">Open Dialog</button>
      <p>
         Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt 
         ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 
         laboris nisi ut aliquip ex ea commodo consequat.
      </p>
      <label for = "textbox">Enter Name: </label>
      <input type = "text">
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −

$ (selector, context).dialog ("action", [params]) Method

The dialog (action, params) method can perform an action on the dialog box, such as closing the box. The action is specified as a string in the first argument and optionally, one or more params can be provided based on the given action.

Basically, here actions are nothing but they are jQuery methods which we can use in the form of string.

Syntax

$(selector, context).dialog ("action", [params]);

The following table lists the actions for this method −

Sr.No. Action & Description
1 close()

This action closes the dialog box. This method does not accept any arguments.

Action - close()

This action closes the dialog box. This method does not accept any arguments.

Syntax

$(".selector").dialog("close");
2 destroy()

This action removes the dialog box competely. This will return the element back to its pre-init state. This method does not accept any arguments.

Action - destroy()

This action removes the dialog box competely. This will return the element back to its pre-init state. This method does not accept any arguments.

Syntax

$(".selector").dialog("destroy");
3 isOpen()

This action checks if the dialog box is open. This method does not accept any arguments.

Action - isOpen()

This action checks if the dialog box is open. This method does not accept any arguments.

Syntax

$(".selector").dialog("isOpen");
4 moveToTop()

This action positions the corresponding dialog boxes to the foreground (on top of the others). This method does not accept any arguments.

Action - moveToTop()

This action positions the corresponding dialog boxes to the foreground (on top of the others). This method does not accept any arguments.

Syntax

$(".selector").dialog("moveToTop");
5 open()

This action opens the dialog box. This method does not accept any arguments.

Action - open()

This action opens the dialog box. This method does not accept any arguments.

Syntax

$(".selector").dialog("open");
6 option( optionName )

This action gets the value currently associated with the specified optionName. Where optionName is the name of the option to get.

Action - option( optionName )

This action gets the value currently associated with the specified optionName. Where optionName is the name of the option to get.

Syntax

var isDisabled = $( ".selector" ).dialog( "option", "disabled" );
7 option()

This action gets an object containing key/value pairs representing the current dialog options hash. This method does not accept any arguments.

Action - option()

This action gets an object containing key/value pairs representing the current dialog options hash. This method does not accept any arguments.

Syntax

var options = $( ".selector" ).dialog( "option" );
8 option( optionName, value )

This action sets the value of the dialog option associated with the specified optionName.

Action - option( optionName, value )

This action sets the value of the dialog option associated with the specified optionName.

Syntax

$(".selector").dialog( "option", "disabled", true );
9 option( options )

This action sets one or more options for the dialog.

Action - option( options )

This action sets one or more options for the dialog.

Syntax

$(".selector").dialog( "option", { disabled: true });
10 widget()

This action returns the dialog box’s widget element; the element annotated with the ui-dialog class name. This method does not accept any arguments.

Action - widget()

This action returns the dialog box’s widget element; the element annotated with the ui-dialog class name. This method does not accept any arguments.

Syntax

$(".selector").dialog("widget");

Example

Now let us see an example using the actions from the above table. The following example demonstrates the use of isOpen(), open() and close() methods.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script type = "text/javascript">
         $(function() {
            $("#toggle").click(function() {
               ($("#dialog-5").dialog("isOpen") == false) ? $(
                  "#dialog-5").dialog("open") : $("#dialog-5").dialog("close") ;
            });
            $("#dialog-5").dialog({autoOpen: false});
         });
      </script>
   </head>
   
   <body>
      <button id = "toggle">Toggle dialog!</button>
      <div id = "dialog-5" title = "Dialog Title!">
         Click on the Toggle button to open and close this dialog box.
      </div>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output −

Event Management on Dialog Box

In addition to the dialog (options) method which we saw in the previous sections, JqueryUI provides event methods as which gets triggered for a particular event. These event methods are listed below −

Sr.No. Event Method & Description
1 beforeClose(event, ui)

This event is triggered when the dialog box is about to close. Returning false prevents the dialog box from closing. It is handy for dialog boxes with forms that fail validation. Where event is of type Event, and ui is of type Object.

Event - beforeClose(event, ui)

This event is triggered when the dialog box is about to close. Returning false prevents the dialog box from closing. It is handy for dialog boxes with forms that fail validation. Where event is of type Event, and ui is of type Object.

Syntax

$(".selector").dialog (
   beforeClose: function(event, ui) {}
);
2 close(event, ui)

This event is triggered when the dialog box is closed. Where event is of type Event, and ui is of type Object.

Event - close(event, ui)

This event is triggered when the dialog box is closed. Where event is of type Event, and ui is of type Object.

Syntax

$(".selector").dialog (
   close: function(event, ui) {}
);
3 create(event, ui)

This event is triggered when the dialog box is created. Where event is of type Event, and ui is of type Object.

Event - create(event, ui)

This event is triggered when the dialog box is created. Where event is of type Event, and ui is of type Object.

Syntax

$(".selector").dialog (
   create: function(event, ui) {}
);
4 drag(event, ui)

This event is triggered repeatedly as a dialog box is moved about during a drag. Where event is of type Event, and ui is of type Object.

Event - drag(event, ui)

This event is triggered repeatedly as a dialog box is moved about during a drag. Where event is of type Event, and ui is of type Object. Possible values of ui are −

  • position − A jQuery object representing the current CSS position of the dialog.

  • offset − A jQuery object representing the current offset position of the dialog.

Syntax

$(".selector").dialog (
   drag: function(event, ui) {}
);
5 dragStart(event, ui)

This event is triggered when a repositioning of the dialog box commences by dragging its title bar. Where event is of type Event, and ui is of type Object.

Event - dragStart(event, ui)

This event is triggered when a repositioning of the dialog box commences by dragging its title bar. Where event is of type Event, and ui is of type Object. Possible values of ui are −

  • position − A jQuery object representing the current CSS position of the dialog.

  • offset − A jQuery object representing the current offset position of the dialog.

Syntax

$(".selector").dialog (
   dragStart: function(event, ui) {}
);
6 dragStop(event, ui)

This event is triggered when a drag operation terminates. Where event is of type Event, and ui is of type Object.

Event - dragStop(event, ui)

This event is triggered when a drag operation terminates. Where event is of type Event, and ui is of type Object. Possible values of ui are −

  • position − A jQuery object representing the current CSS position of the dialog.

  • offset − A jQuery object representing the current offset position of the dialog.

Syntax

$(".selector").dialog (
   dragStop: function(event, ui) {}
);
7 focus(event, ui)

This event is triggered when the dialog gains focus. Where event is of type Event, and ui is of type Object.

Event - focus(event, ui)

This event is triggered when the dialog gains focus. Where event is of type Event, and ui is of type Object.

Syntax

$(".selector").dialog (
   focus: function(event, ui) {}
);
8 open(event, ui)

This event is triggered when the dialog box is opened. Where event is of type Event, and ui is of type Object.

Event - open(event, ui)

This event is triggered when the dialog box is opened. Where event is of type Event, and ui is of type Object.

Syntax

$(".selector").dialog (
   open: function(event, ui) {}
);
9 resize(event, ui)

This event is triggered repeatedly as a dialog box is resized. Where event is of type Event, and ui is of type Object.

Event - resize(event, ui)

This event is triggered repeatedly as a dialog box is resized. Where event is of type Event, and ui is of type Object.Possible values of ui are −

  • originalPosition − A jQuery object representing CSS position of the dialog prior to being resized.

  • position − A jQuery object representing the current CSS position of the dialog.

  • originalSize − A jQuery object representing the size of the dialog prior to being resized.

  • size − A jQuery object representing the current size of the dialog.

Syntax

$(".selector").dialog (
   resize: function(event, ui) {}
);
10 resizeStart(event, ui)

This event is triggered when a resize of the dialog box commences. Where event is of type Event, and ui is of type Object.

Event - resizeStart(event, ui)

This event is triggered when a resize of the dialog box commences. Where event is of type Event, and ui is of type Object.Possible values of ui are −

  • originalPosition − A jQuery object representing CSS position of the dialog prior to being resized.

  • position − A jQuery object representing the current CSS position of the dialog.

  • originalSize − A jQuery object representing the size of the dialog prior to being resized.

  • size − A jQuery object representing the current size of the dialog.

Syntax

$(".selector").dialog (
   resizeStart: function(event, ui) {}
);
11 resizeStop(event, ui)

This event is triggered when a resize of the dialog box terminates. Where event is of type Event, and ui is of type Object.

Event - resizeStop(event, ui)

This event is triggered when a resize of the dialog box terminates. Where event is of type Event, and ui is of type Object.Possible values of ui are −

  • originalPosition − A jQuery object representing CSS position of the dialog prior to being resized.

  • position − A jQuery object representing the current CSS position of the dialog.

  • originalSize − A jQuery object representing the size of the dialog prior to being resized.

  • size − A jQuery object representing the current size of the dialog.

Syntax

$(".selector").dialog (
   resizeStop: function(event, ui) {}
);

The following examples demonstrate the use of the events listed in the above table.

Use of beforeClose Event method

The following example demonstrates the use of beforeClose event method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
         .invalid { color: red; }
         textarea {
            display: inline-block;
            width: 100%;
            margin-bottom: 10px;
         }
      </style>
      
      <!-- Javascript -->
      <script type = "text/javascript">
         $(function() {
            $( "#dialog-6" ).dialog({
	       autoOpen: false, 
               buttons: {
                  OK: function() {
                     $( this ).dialog( "close" );
                  }
               },
               beforeClose: function( event, ui ) {
                  if ( !$( "#terms" ).prop( "checked" ) ) {
                     event.preventDefault();
                     $( "[for = terms]" ).addClass( "invalid" );
                  }
               },
               width: 600
            });
            $( "#opener-5" ).click(function() {
               $( "#dialog-6" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <div id = "dialog-6">
         <p>You must accept these terms before continuing.</p>
         <textarea>This Agreement and the Request constitute the entire agreement of the 
         parties with respect to the subject matter of the Request. This Agreement shall be 
         governed by and construed in accordance with the laws of the State, without giving 
         effect to principles of conflicts of law.</textarea>
         <div>
            <label for = "terms">I accept the terms</label>
            <input type = "checkbox" id = "terms">
         </div>
      </div>
      <button id = "opener-5">Open Dialog</button>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output −

Use of resize Event method

The following example demonstrates the use of resize event method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionality</title>
      <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px solid #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script type = "text/javascript">
         $(function() {
            $( "#dialog-7" ).dialog({
               autoOpen: false, 
               resize: function( event, ui ) {
                  $( this ).dialog( "option", "title",
	         ui.size.height + " x " + ui.size.width );
               }
            });
            $( "#opener-6" ).click(function() {
               $( "#dialog-7" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <div id = "dialog-7" title = "Dialog Title goes here..."
         >Resize this dialog to see the dialog co-ordinates in the title!</div>
      <button id = "opener-6">Open Dialog</button>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output −

Extension Points

The dialog widget is built with the widget factory and can be extended. To extend widgets, we can either override or add to the behavior of existing methods. Following method provides as extension point with the same API stability as the dialog methods. Listed in the above table.

Sr.No. Method & Description
1 _allowInteraction(event)

This method allows the user to interact with a given target element by whitelisting elements that are not children of the dialog but allow the users to be able to use. Where event is of type Event.

Extension Point - allowInteraction(event, ui)

This method allows the user to interact with a given target element by whitelisting elements that are not children of the dialog but allow the users to be able to use. Where event is of type Event.

Code Example

_allowInteraction: function( event ) {
  return !!$( event.target ).is( ".select2-input" ) || this._super( event );
}
  • Select2 plugin is used within modal dialogs

  • super() call ensures elements within the dialog can still be interacted with.

The jQuery UI Widget Factory is an extensible base on which all of jQuery UI's widgets are built. Using the widget factory to build a plugin provides conveniences for state management, as well as conventions for common tasks like exposing plugin methods and changing options after instantiation.



Advertisements
E-Books Store