To make the creation of dialog boxes, the <dialog> tag was introduced. The general use of this tag is to put a form inside the dialog. Below is an example of setting up the HTML portion of a dialog box. You will notice that I am setting the form method to dialog. This causes the button to close the dialog box instead of submitting the form. If you want the form to be submitted, but also want an option to close the dialog box without submitting then you can use the normal get or post methods and have the button you want for closing the dialog box to have the formmethod attribute be set to dialog.
If you wanted to run the above you would see nothing as the dialog does not appear until you show it. To show the dialog you have two choices. You can use the show() method or the showModal() method. Both bring up the same dialog box, but the behavior of that dialog box will be different. As mentioned in the previous section, modeless dialog boxes let you keep interacting with the page beneath the dialog box while modal dialog boxes require that you close them before you can do anything else on the page. Here is an example of opening our dialog box as a modal dialog box.
If you have other things on the page that you can interact with, you will notice that they do not show any sign that they are disabled but you can’t interact with them.
There is another interesting behavior that forms have and that is with required fields. As we will be exploring next section, a required field must be filled out before a form can be submitted. This also happens to mean that you can not close a form until all the inputs that have the required attribute set need to be filled out as can be seen in this example.
You will notice that event handlers on the form buttons are allowed, which should go without saying. This lets you do things with the form fields once the form has been closed. Even though the input fields are no longer visible, they are still accessible using the document class.
Before closing out our discussion of dialogs, it should be pointed out that you do not need to have a form inside the dialog tag. The big issue here is that the button needs to be closed in the button’s event handler by calling the dialog elements’ close() method. Here is an example of a dialog box that does not have any form inside of it.
The code for handling the result is where we close the dialog box.
We are using forms for getting data, and as we intend to process the data ourselves, it is important that we understand a bit about form validation which we will look at next.
This chapter's summary cheat sheet.
How forms are traditionally used.
How to set up a form in HTML.
Getting text input from the user.
Making your input fields more specific and semantic.
Making forms look more like printed forms.
How to use Checkboxes and Radio buttons.
Using numbers and dates gives your user better GUI controls.
The reset, button, image, color, and file input types.
The select element lets users select options from a drop-down list.
The datalist elements lets you create pull-down lists for input elements.
Displaying values in a form.
Built in dialog boxes and pre-dialog tag handmade dialogs.
The <dialog> tag and using it to create dialog boxes.
Review of validating forms in HTML.
How to use JavaScript to prevent invalid forms from being submitted.
Coming December 8th.
Coming December 15th.