Output is a bit of an oddity since you use it to put output results into your form using JavaScript. This could just as easily be done with a span element which has even more power and flexibility. However, output was created for use in forms and as such allows a designer to create forms that are independent of the other HTML elements.
There is not very much to the output tag so this will be a very short section of this chapter but is presented here out of completeness. Simply put, the value of the output tag represents the text that will be shown. The idea behind this is that if you have a form that is performing some type of calculations it is possible to have those results shown to the user without requiring that the form is submitted as a preview of the submission.
Here is a simple example, where we are using our range elements with the results of the three ranges being shown using output in the form. We are going to take advantage of the fact that we are using a form to listen to the form’s input event which triggers whenever any of the forms controls are updated. Here is the HTML portion:
By itself update does not do much but requires that its value be set before anything is displayed. This is handled by JavaScript which performs the calculations and sets the results. Here is the code for handling this:
Resulting in a simple demo. While I can see using the tag to keep form content clean of HTML, with the ability to use spans and other html tags it is more of an antique element.
More modern items are dialogs. These were a recent addition to HTML5, though predate the addition by at least a decade as many developers faked dialog boxes which is what we will be looking 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.
Chapter project: Trivia game
Solution for the Trivia gaame