jQuery UI Dialog – Tricks for New Players
There are a number of good dialog plugins for jQuery out there. I’ve looked at Facebox, SimpleModal most recently. But there’s really no need to include additional overhead into your scripts and possibly start reinventing the wheel if all you need is literally a dialog popup window.
jQuery UI comes with an incredibly simple to use dialog control (among others) straight “out-of-the-box”. I’m going to look at how to include it in my own solution and what some of the behaviours are that can cause a great deal of head-scratching for the uninitiated.
If you haven’t already, have a look at jQuery UI; in particular the ThemeRoller utility. Play around with some styles and download a theme (or just download one of the many pre-baked ones available). I’m going to gloss over the part about how to actually implement the dialog fairly quickly – there are two tricks that I want to hit on in more detail afterwards: a design consideration for .Net developers and a native behavioural attribute that you should be aware of when working with the UI dialog.
Engaging in Meaningful Dialog
Creating a dialog with jQuery UI is incredibly straight-forward. I need only one element and two attributes: the ID and Title. Let’s look at the “out-of-the-box” example provided with your ThemeRoller download:
<div id="dialog" title="Dialog Title"> 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. </div>
This block will form the entire dialog. The element is the dialog container and everything else within it is defined by your own individual requirements. The title bar of the dialog retrieves its text from, appropriately, the title attribute of the element.
$('#dialog').dialog();
What? You expected more? That’s all you need. Now, there are plenty of additional options available such as defining buttons, the dimensions of the dialog and other behaviours: read the documentation available at http://jqueryui.com for a full treatment on these options.
The final step is to wire up an event handler for something to trigger the display of our dialog. Again, we’re going to refer to the example downloaded with ThemeRoller:
<p><a href="#" id="dialog_link" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-newwin"></span>Open Dialog</a></p>
Technically, we don’t need a lot of that extra fluff. This would work just as happily:
<p id="dialog_link">Open Dialog</p>
All we need is one last item of jQuery:
$('#dialog_link').click(function(){
$('#dialog').dialog('open');
return false;
});
So we now have a click event wired up to the element with ID “dialog_link”. You can try this out for yourself now – it should all work just fine (don’t forget to include references to the style sheets).
The Gotcha!
Note the two separate jQuery methods we used here: one to set the actual definition of the dialog and another to set up the events for showing it. The key here is the call for defining the dialog – the initialisation call. The following might seem like a good idea, but will not work:
$('#dialog_link').click(function(){
$('#dialog').dialog();
return false;
});
It is not sufficient just to say “when I click #dialog_link, show #dialog as a dialog”. Why? Because the .dialog() syntax is an init call. When you close that dialog, it’s still there, just hidden! Regardless of what options you place within that initialisation call, remember that it only needs to be initialised once. Wiring an initialisation to the click event means that you will initialise the dialog on that first click and it won’t need initialised again. When you close the dialog, you can’t get it back because you’re trying to call init() again.
So what do we want to do instead? The best option is to follow the example set in your original download. Initialise your dialog first (you’ll probably also want to set the “autoopen” option to false) and then wire up the click:
$("#dialog").dialog({ autoOpen: false });
$("#dialog_link").click(function() {
$("#dialog").dialog("open");
});
That was possibly a rather long-winded explanation, but hopefully a clear one.
Working with .Net
As I mentioned earlier, there is also something to be aware of if you’re using the jQuery UI dialog in a .Net solution.
If you have a look at the HTML of your downloaded example, you’ll see that the dialog content appears roughly half-way down our page. Now have a look at this screen grab once the jQuery script has initialised the actual dialog.
You’ll see that the content we had mid-way through our page has been marked up with additional classes and, most importantly, placed at the bottom of the page immediately before the closing tag. Why is this important? Because it also means that any ASP.Net controls you place within this dialog will also appear at the bottom of the page, outside of the page’s tag. This means you won’t be able to get a handle to them on postback.
What’s the solution? Well, there are two options:
- Move the elements back to the form, and manually submit when the button is clicked
- Clone the elements when you create the dialog, then clone the values back, trigger click on the original button (or, if you only have one or two values to post back, simply assign the values to an ASP.Net hidden field control).
Hope that helps and hat-tip to Richard Worth for providing personal input to the user community!




Some of the advice here is wrong. If your only looking for dialog functionality then including the ui core and dialog js files and the css is much more overhead (120 kb minified) than just using blockui or simplemodal. If you are harnessing other ui widgets such as datepicker then fair enough but the overhead for just the dialog is large. Phil did you actually do any research into this?
That’s a very fair – albeit somewhat condescending – point.
I perhaps haven’t structured my language as well as I might have. The Dialog control comes bundled with the jQuery UI package when you use ThemeRoller. This article aims to look solely at that control at the exclusion of any others.
Your point that the entire library for the sake of one control being overkill is quite valid. The total size of the scripts involved would not be worth it – you’d be better off just getting the core jQuery library and a dialog plugin. However, as you say – if you were using a number of controls and wanted to consistently theme theme with the supplied ThemeRoller stylesheets, then ThemeRoller is likely to be your easier and faster option.
@redsquare I think 120 kb minified might be overstating just a bit. For just jQuery UI Dialog, it’s 20kb minified js, 22 kb unminified css, 24 kb images. 66 kb total. If you want draggable and resizable (which are optional) that will get you up to 121 kb, yes, but other plugins (which you’re comparing to) don’t have these features.
A better comparison might be if you were to use jQuery UI Dialog’s 20kb js with your own custom 1kb css (see http://docs.jquery.com/UI/Dialog#theming). That’s still about the same size as these other two combined, but a good order of magnitude less than your estimate.
I would love to help you with your poll but… what the hell are you talking about it? I dont understand a thing.
)))
Ha! That’s ok, Jana. It’s the thought that counts. Unless you’re in the industry and are following new concepts reasonably closely, this whole blog is going to be largely meaningless.
what about if you are also using MS Ajax control toolkit and have a checkbox outside of a dailog that submits the form. If I use the jquery dialog, the form will fail giving “htmfile unknown error’. This is (so far as I can tell) ONLY when I have a duplicate form on the page(such as inside the dialog).
Oo. Not too sure. It feels to me as though it’s not actually jQuery that would be causing the problem (although it would likely be worthwhile making sure there is no conflict with the two libraries by setting jQuery’s
$.noConflict()option (http://docs.jquery.com/Using_jQuery_with_Other_Libraries).The jQuery dialog is just a block of HTML, so what else is going on within your page? Unless you’re using MVC, you shouldn’t be using two
tags. Do you have a code sample?The problem with jQuery dialog use within a form is that by default when the dialog is created it is removed from it’s original location in the DOM and appended to the BODY of the document. So when you submit from the dialog you are going to be missing values that the server expects from the request.
Very nice and useful tutorials for web designers,
Thanks for posting.
This will also help you.
http://praveenbattula.blogspot.com/2009/08/jquery-dialog-open-only-once-solution.html