Home

How to Create a Custom Dialog Box in AJAX for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:12:52
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

AJAX lets HTML5 and CSS3 programmers make custom dialog boxes. JavaScript supplies a few dialog boxes (the alert and prompt dialog boxes), but these are quite ugly and relatively inflexible. The jQuery UI includes a technique for turning any div into a virtual dialog box. The dialog box follows the theme and is resizable and movable.

image0.jpg

Building the dialog box is not difficult, but you need to be able to turn it on and off with code, or it will not act like a proper dialog box (which mimics a window in the operating system):

  1. Create the div you intend to use as a dialog box.

    Create a div and give it an ID so that you can turn it into a dialog box node. Add the title attribute, and the title shows up in the dialog box's title bar.

      <div id = "dialog"
       title = "my dialog">
      <p>
       The dialog class allows you to have a movable, sizable
       customized dialog box consistent with the installed
       page theme.
      </p>
      </div>
  2. Turn the div into a dialog box.

    Use the dialog() method to turn the div into a jQuery dialog box node in the init() function:

      $("#dialog").dialog();
  3. Hide the dialog box by default.

    Usually you don't want the dialog box visible until some sort of event happens. In this particular example, you may not want the dialog box to appear until the user clicks a button. You can put some code to close the dialog box in the init() function so that the dialog box will not appear until it is summoned.

  4. Close the dialog box.

    To close a dialog box, refer to the dialog box node and call the dialog() method on it again. This time, send the single value “close” as a parameter, and the dialog box will immediately close:

      //initially close dialog
      $("#dialog").dialog("close");
  5. Clicking the X automatically closes the dialog box.

    The dialog box has a small X that looks like the Close Window icon on most windowing systems. The user can close the dialog box by clicking this icon.

  6. You can open and close the dialog box with code.

    My Open Dialog and Close Dialog buttons call functions that control the behavior of the dialog box. For example, here is the function attached to the Open Dialog button:

     function openDialog(){
      $("#dialog").dialog("open");
     } // end openDialog

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].