Before reading this article, I highly recommend reading the previous part of the series:
- JQueryUI - Day 1
- JQueryUI - Day 2 (Draggable)
- JQueryUI - Day 3 (Droppable)
- JQueryUI - Day 4 (Resizable)
- JQuery UI - Day 5 (Selectable)
- JQuery UI - Day 6 (Accordion)
- JQuery UI - Day 7 (AutoComplete)
- JQueryUI - Day 8 (Datepicker)
jQuery Dialog opens content in an interactive overlay. Dialog boxes are one of the nicest ways of presenting information on an HTML page. We can define dialog as a floating window with a title and content that can be moved in any direction or may be resized.
Syntax:
$(selector, context).dialog(options);
Or
$(selector, context).dialog("action", [params]);
Example:
- <!DOCTYPEhtml>
- <head runat="server">
- <title></title>
- <link href="http://code.jquery.com/ui/1.10.4/themes/blitzer/jquery-ui.css" rel="stylesheet">
- <script src="http://code.jquery.com/jquery-1.10.2.js">
- </script>
- <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
- </script>
- <script>
- $(document).ready(function()
- {
- $("#dialog-1").dialog({
- autoOpen: false,
- buttons: [
- {
- text: "Ok",
- icons:
- {
- primary: " ui-icon-mail-closed"
- },
- click: function() {
- $(this).dialog("close");
- }
- }
- ]
- });
- $("#show").click(function()
- {
- $("#dialog-1").dialog("open");
- });
- });
- </script>
- <style>
- #datepicker-1 {
- margin-top: 20px;
- }
- #div1 {
- background-color: coral;
- color: ButtonHighlight;
- font-size: 30px;
- height: 450px;
- width: 1000px;
- text-align: center;
- margin-left: 150px
- }
- #show {
- font-size: 20px;
- background-color: #14E058
- }
- </style>
- </head>
- <body>
- <div id="div1">
- <span>************Dialog*************</span>
- <div class="ui-widget">
- <div id="dialog-1">
- <h1>Dialog Box</h1>
- <br/>
- <span>This is a simple dialog Example</span>
- </div>
- <input id="show" type="button" value="Show Dialog" "/>
- </div>
- </div>
- </body>
- </html>
Output:

Options:
Option | Description | Example |
autoOpen | If set to | $("#dialog-1").dialog({ autoOpen: false }); |
button | Specifies which buttons should be displayed on the dialog. These are listed as objects, and each property is the text on the button. | $("#dialog-1").dialog({ buttons: [ { text: "Ok", icons: { primary: " ui-icon-mail-closed" }, click: function () { $(this).dialog("close"); } } ] }); |
closeOnEscape | Specifies whether the dialog should close when it has focus and the user presses the escape (ESC) key. | $("#dialog-1").dialog({ closeOnEscape: false }); |
closeText | Specifies the text for the close button. | $("#dialog-1").dialog({ closeText:"Close" }); |
dialogClass | The specified class name(s) will be added to the dialog, for additional theming. | $("#dialog-1").dialog({ dialogClass: "alert" }); |
draggable | If set to false dialog box will not be draggable by clicking and dragging the title bar. | $("#dialog-1").dialog({ draggable: false }); |
height | The height of the dialog. | $("#dialog-1").dialog({ height: 300 }); |
hide | This option is used to set the effect to be used when the dialog box is closed. By default its value is null. | $("#dialog-1").dialog({ hide: { effect:"explode", duration: 1300 } }); |
maxHeight | The maximum height to which the dialog can be resized. | $("#dialog-1").dialog({ maxHeight: 400 }); |
maxWidth | The maximum width to which the dialog can be resized. | $("#dialog-1").dialog({ maxWidth: 400 }); |
minHeight | The minimum height to which the dialog can be resized. | $("#dialog-1").dialog({ minHeight: 600 }); |
minWidth | The minimum width to which the dialog can be resized | $("#dialog-1").dialog({ minWidth: 600 }); |
modal | If this option is set to true the dialog will have modal behavior and other items on the page will be disabled. | $("#dialog-1").dialog({ modal:true }); |
position | Specifies where the dialog should be displayed when opened. Default value is | $("#dialog-1").dialog({ position:{ my: "center", at:"center", of: window } }); |
resizable | If set to true then dialog box can be resized. By default its value is true. | $("#dialog-1").dialog({ resizable: false }); |
show | Define If and how to animate the showing of the dialog. By default its value is null. | $("#dialog-1").dialog({ show: { effect: "blind", duration: 500 } }); |
title | Specifies the title of the dialog. | $("#dialog-1").dialog({ title: "Dialog Box" }); |
width | The width of the dialog in pixels. | $("#dialog-1").dialog({ width:400 }); |
Methods:
Method | Description | Example |
close() | Closes the dialog. | $("#dialog-1").dialog("close"); |
destroy() | Removes the dialog functionality completely. This will return the element back to its pre-init state. | $("#dialog-1").dialog("destroy"); |
instance() | Retrieves the dialog's instance object. | Varobj=$("#dialog-1").dialog("instance"); |
isOpen() | Check whether the dialog is currently open. | Varobj=$("#dialog-1").dialog("isOpen"); |
moveToTop() | Moves the dialog to the top of the dialog stack. | $("#dialog-1").dialog("moveToTop"); |
open() | Opens the dialog. | $("#dialog-1").dialog("open"); |
option(option_Name) | Gets the value currently associated with the specified | Varobj=$("#dialog-1").dialog("maxHeight"); |
option() | Gets an object containing key/value pairs representing the current dialog options | Varobj=$("#dialog-1").dialog(); |
option(option_name,value) | Sets the value of the dialog option associated with the specified | Varobj=$("#dialog-1").dialog("maxHeight",100); |
widget() | Returns a | $("#dialog-1").dialog("widget"); |
Events:
Event | Description | Example |
beforeClose(event,ui) | Event triggered when a dialog is about to close. If canceled, the dialog will not close | $("#dialog-1").dialog({ beforeClose: function (event, ui) { }}); |
close(event,ui) | Event triggered when the dialog is closed. | $("#dialog-1").dialog({ close: function (event, ui) { }}); |
create(event,ui) | Event triggered when the dialog is closed. | $("#dialog-1").dialog({ create: function (event, ui) { }}); |
drag(event,ui) | Event triggered while the dialog is being dragged. | $("#dialog-1").dialog({ drag: function (event, ui) { }}); |
dragStart(event,ui) | Event triggered when the user starts dragging the dialog. | $("#dialog-1").dialog({ dragStart: function (event, ui) { }}); |
dragStop(event,ui) | Event triggered after the dialog has been dragged. | $("#dialog-1").dialog({ dragStop: function (event, ui) { }}); |
focus(event,ui) | Event triggered when the dialog gains focus. | $("#dialog-1").dialog({ focus: function (event, ui) { }}); |
open(event,ui) | Event triggered when the dialog is opened. | $("#dialog-1").dialog({ open: function (event, ui) { }}); |
resize(event,ui) | Event is triggered repeatedly as a dialog box is resized. | $("#dialog-1").dialog({ resize: function (event, ui) { }}); |
resizeStart(event,ui) | Event is triggered when a resize of the dialog box start. | $("#dialog-1").dialog({ resizeStart: function (event, ui) { }}); |
resizeStop(event,ui) | Event is triggered when a resize of the dialog box terminates. | $("#dialog-1").dialog({ resizeStop: function (event, ui) { }}); |
Let us take some example.
Example 1:
- <!DOCTYPE html>
- <head runat="server">
- <title></title>
- <link href="http://code.jquery.com/ui/1.10.4/themes/blitzer/jquery-ui.css" rel="stylesheet">
- <script src="http://code.jquery.com/jquery-1.10.2.js">
- </script>
- <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
- </script>
- <script>
- $(document).ready(function() {
- $("#dialog-1").dialog({
- autoOpen: false,
- show: "puff",
- hide: "slide",
- closeOnEscape: false,
- title: "Hello!"
- });
- $("#show").click(function() {
- $("#dialog-1").dialog("open");
- })
- });
- </script>
- <style>
- #datepicker-1 {
- margin-top: 20px;
- }
- #div1 {
- background-color: coral;
- color: ButtonHighlight;
- font-size: 30px;
- height: 450px;
- width: 1000px;
- text-align: center;
- margin-left: 150px
- }
- #show {
- font-size: 20px;
- background-color: #14E058
- }
- </style>
- </head>
- <body>
- <divid="div1">
- <span>************Dialog*************</span>
- <divclass="ui-widget">
- <divid="dialog-1">
- <h1>Dialog Box</h1>
- <br/>
- <span>This is a simple dialog Example</span>
- </div>
- <inputid="show" type="button" value="Show Dialog" "/>
- </div>
- </div>
- </body>
- </html>
Output:

When we click on “Show Dialog” button , a dialog box will be visible. The title of the dialog box is “Hello!” and the dialog box is visible in “puff” animation style. When we click on the cross button this dialog box become closed with slide animation effect.
Example 2:
- <!DOCTYPE html>
- <head runat="server">
- <title></title>
- <link href="http://code.jquery.com/ui/1.10.4/themes/blitzer/jquery-ui.css" rel="stylesheet">
- <script src="http://code.jquery.com/jquery-1.10.2.js">
- </script>
- <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
- </script>
- <script>
- $(document).ready(function() {
- $("#dialog-1").dialog({
- autoOpen: false,
- show: "puff",
- hide: "slide",
- closeOnEscape: false,
- title: "Hello!",
- height: 400,
- width: 400,
- buttons: [
- {
- text: "Ok",
- icons: {
- primary: " ui-icon-mail-closed"
- },
- click: function() {
- $(this).dialog("close");
- }
- }
- ]
- });
- $("#ui-datepicker-year").datepicker({
- autoSize: true,
- showOn: "button",
- buttonImage: "/jquery-ui-1.11.4.custom/images/datepicker.gif",
- changeMonth: true
- });
- $("#show").click(function() {
- $("#dialog-1").dialog("open");
- })
- });
- </script>
- <style>
- #datepicker-1 {
- margin-top: 20px;
- }
- #div1 {
- background-color: coral;
- color: ButtonHighlight;
- font-size: 30px;
- height: 450px;
- width: 1000px;
- text-align: center;
- margin-left: 150px
- }
- #show {
- font-size: 20px;
- background-color: #14E058
- }
- </style>
- </head>
- <body>
- <div id="div1">
- <span>************Dialog*************</span>
- <div class="ui-widget">
- <divid="dialog-1">
- <h1>Select Date</h1>
- <br/>
- <div class="ui-widget">
- <input type="text" id="ui-datepicker-year" />
- </div>
- </div>
- <input id="show" type="button" value="Show Dialog" "/>
- </div>
- </div>
- </body>
- </html>
Output:

In the above example we are displaying a datepicker in dialog box. We also define a button for the dialog box and we defined the text and icon for this button. When we click on this button the dialog box will be closed.
Example 3:
- <!DOCTYPE html>
- <head runat="server">
- <title></title>
- <link href="http://code.jquery.com/ui/1.10.4/themes/blitzer/jquery-ui.css" rel="stylesheet">
- <script src="http://code.jquery.com/jquery-1.10.2.js">
- </script>
- <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">
- </script>
- <script>
- $(document).ready(function() {
- var States = [
- {
- label: "Andra Pradesh",
- value: "ANP",
- desc: "Capital is Hyderabad"
- },
- {
- label: "Arunachal Pradesh",
- value: "ARP",
- desc: "Capital is Itangar"
- },
- {
- label: "Bihar",
- value: "BIH",
- desc: "Capital is Patna"
- },
- {
- label: "Chhattisgarh",
- value: "CHT",
- desc: "Capital is Raipur"
- },
- {
- label: "Goa",
- value: "GOA",
- desc: "Capital is Panaji"
- },
- {
- label: "Haryana",
- value: "HAR",
- desc: "Chandigarh"
- },
- {
- label: "Jammu and Kashmir",
- value: "JAK",
- desc: "Srinagar and Jammu"
- },
- {
- label: "Kerala",
- value: "KRL",
- desc: "Capital is Thiruvananthapuram"
- },
- {
- label: "Mizoram",
- value: "MIZ",
- desc: "Capital is Aizawi"
- },
- {
- label: "Rajasthan",
- value: "RAJ",
- desc: "Capital is Jaipur"
- },
- {
- label: "Sikkim",
- value: "SKM",
- desc: "Capital is Gangtok"
- },
- {
- label: "Uttaranchal",
- value: "UTH",
- desc: "Capital is Dehradun"
- },
- {
- label: "West Bengal",
- value: "WGL",
- desc: "Capital is Kolkata"
- },
- ];
- $("#dialog-1").dialog({
- autoOpen: false,
- show: "puff",
- hide: "slide",
- closeOnEscape: false,
- title: "Hello!",
- height: 400,
- width: 400,
- buttons: [
- {
- text: "Ok",
- icons: {
- primary: " ui-icon-mail-closed"
- },
- click: function() {
- $(this).dialog("close");
- }
- }
- ]
- });
- $("#ui-datepicker-year").datepicker({
- autoSize: true,
- showOn: "button",
- buttonImage: "/jquery-ui-1.11.4.custom/images/datepicker.gif",
- changeMonth: true
- });
- $("#show").click(function() {
- $("#dialog-1").dialog("open");
- })
- $("#automplete-1").autocomplete({
- source: States,
- autoFocus: true,
- minLength: 2,
- delay: 400,
- position: {
- my: "right top",
- at: "right bottom",
- collision: "none"
- },
- focus: function(event, ui) {
- $("#automplete-1").val(ui.item.value);
- },
- select: function(event, ui) {
- $("#automplete-1").val(ui.item.value);
- $("#auto_id").val(ui.item.label);
- returnfalse;
- }
- })
- .data("ui-autocomplete")._renderItem = function(ul, item) {
- return $("<li>").append("<a>" + item.label + "<br>" + "<i style=\"color:#1ACE26\">" + item.desc + "</i>" + "</a>")
- .appendTo(ul);
- };
- });
- </script>
- <style>
- #datepicker-1 {
- margin-top: 20px;
- }
- #div1 {
- background-color: coral;
- color: ButtonHighlight;
- font-size: 30px;
- height: 450px;
- width: 1000px;
- text-align: center;
- margin-left: 150px
- }
- #show {
- font-size: 20px;
- background-color: #14E058
- }
- #automplete-1 {
- margin-left: 120px
- }
- </style>
- </head>
- <body>
- <div id="div1">
- <span>************Dialog*************</span>
- <div class="ui-widget">
- <div id="dialog-1">
- <h1>Select State</h1>
- <br/>
- <div class="ui-widget">
- <input type="text" id="automplete-1" />
- <input type="hidden" id="auto_id">
- </div>
- </div>
- <input id="show" type="button" value="Show Dialog" "/>
- </div>
- </div>
- </body>
- </html>
Output:

In above example we are showing autocomplete in dialog box.

Santhakumar MunuswamyPosted Jan 17, 2016, 2:28 PM
Thanks for nice share
Muhammad Aqib ShehzadPosted Jan 17, 2016, 2:54 AM
good one
Shubham KumarPosted Jan 16, 2016, 1:02 PM
nice! not read fully but i do asap :)
Pankaj Kumar ChoudharyPosted Jan 15, 2016, 8:21 PM
Thanks Nanddeep Nachan Sir...........
Pankaj Kumar ChoudharyPosted Jan 15, 2016, 8:20 PM
Thanks Debasis Saha Sir...........
Pankaj Kumar ChoudharyPosted Jan 15, 2016, 8:20 PM
Thanks Gowtham K Sir..........
Nanddeep NachanPosted Jan 15, 2016, 8:12 AM
Nice share
Debasis SahaPosted Jan 15, 2016, 6:17 AM
Good One..
Gowtham KPosted Jan 14, 2016, 8:51 PM
Good One, Thanks for sharing