Creating modal dialogs with overlay
This demo shows you how you can use the overlay tool to prompt for user input. You may be familiar with the old school prompt() call in JavaScript. Now let's get into the modern world and use JavaScript more innovatively:
This is a modal dialog
You can only interact with elements that are inside this dialog. To close it click a button or use the ESC key.
This is a modal dialog
You can only interact with elements that are inside this dialog. To close it click a button or use the ESC key.
The "Modal dialog" is an overlay that requires the user to interact with it before they can return to using the main document. This feature can be achieved with the Expose tool which is tightly integrated with this overlay tool.
There are two major advantages over the default JavaScript prompts:
- You can tweak the look and feel of the dialog box
- You can ask any kind of questions and have multiple input fields.
HTML coding
Again, the HTML coding is no different from the basic setup. There are trigger elements and their corresponding overlay elements. Note that we have explicitly defined the close buttons here by assigning the class name "close" to the buttons. This will automatically bind the close actions to them.
CSS coding
Nothing special here. We style the dialog and a few elements inside it:
JavaScript coding
First we enable those trigger elements to open our dialogs. We select the elements with the jQuery selector .modalInput and initialize overlaying with two configuration variables. The mask variable enables the semitransparent mask and we give it the color "#ebecff". By setting closeOnClick variable to false we enable our "modal" feature: users cannot interact with the main document when the overlay is open because the rest of the document is disabled by the mask until the dialog is closed.
Yes/No prompt
Here is the code for the yes/no prompt. We bind a click event to both buttons and we know which one of the buttons is clicked by using jQuery's index method. After we know the result we simply write the answer to the trigger. Of course you should do something more useful with the result.
Input prompt
Here we bind a submit event handler for the FORM element inside the dialog. This handler closes the overlay by using the overlay API call close. The default form submission action can be cancelled by returning e.preventDefault().