Form structure
Form elements can be used outside the form element, but they will not do anything on their own, and the behaviour must be customised with JavaScript. I.e. you need to add an event listener that triggers on changes in the element (more on that in later chapters). In this chapter we will only concern ourselves with the <form>
element and elements inside it. Presented here are the elements <fieldset>
, <legend>
, <label>
, <output>
and <input>
.
Grouping and labeling
The element <fieldset>
is used to create groups of elements within a form that share the same purpose. This can be to group radio buttons together, or several input fields that belong together, e.g. all the fields that make up the address (name, street address, city, zip code, country). A <fieldset>
can be labeled with a <legend>
element. This element describes the purpose of the <fieldset>
. Assistive technologies, such as screen readers, use <legend>
to label widgets inside the <fieldset>
element.
The <label>
element is used to label a widget, describing what the content for the form element should be, e.g. name or e-mail. The <label>
can have a attribute for
, which links the label to the widget with the same id
. Using the for
attribute lets assistive technologies read your page. That is not the only reason for using the for
attribute though. With it, users can click the label to active the corresponding widget. This is especially useful for radio buttons and checkboxes.
You can see <fieldset>
, <legend>
and <label>
used below, together with the <input>
element. If you want the label to be placed after the <input>
element, you should move it below the element, but within the <label>
element. <br>
is used to place the next <label>
on a new line, rather than on the same line. This is not done for the widgets city and zip, thus they will be on the same line.
Output
The <output>
is used to store the result from a calculation, e.g. from several other <input>
elements. You can use the for
attribute to list the IDs of other elements that contribute input values to the calculation. These IDs must be divided by a space
Input
The <input>
element is the most essential element in the form, as it is the source of information from the user. This element can change radically, dependent on what value the type
attribute is given. In the example below, the attribute text
is given the value text
, which will give a text field the user can fill out.
The value for the type
attribute can be categorised into four categories: single line text fields, controls without text input, time and date controls, and buttons. The <input>
element supports many attributes, but not all are relevant for all type
values. Below is a table summarising the relevant attributes.
The table above is a subset of the table found at [How to structure an HTML form, from Mozilla Developer Network
If the browser doesn't support the value in the type
attribute, the input field will be rendered as a text
input.
Below is an explanation for the attributes with examples. The attributes for the buttons are linked to an external resource, as they are out of scope for this course.
<span id="accept">accept</span>
If the value of the type attribute is file, then this attribute will indicate the types of files that the server accepts, otherwise it will be ignored. The value must be a comma-separated list of unique content type specifiers:
A file extension starting with the STOP character (e.g. .jpg, .mp4, .wav)
audio: representing audio files
video: representing video files
image: representing image files
<span id="autocomplete">autocomplete</span>
This attribute indicates whether the value of the control can be automatically completed by the browser.
<span id="checked">checked</span>
When the value of the type attribute is radio or checkbox, the presence of this Boolean attribute indicates that the control is selected by default, otherwise it is ignored.
<span id="list">list</span>
Identifies a list of pre-defined options to suggest to the user. The value must be the id of a <datalist>
element in the same document. The browser displays only options that are valid values for this input element.
The most known use case for this is an autocompletion list for text fields. The values available are set with <option>
elements within the <datalist>
element.
<span id="max">max</span>
The maximum (numeric or date-time) value for this item, which must not be less than its minimum (min attribute) value.
<span id="maxlength">maxlength</span>
If the value of the type attribute is text
, email
, search
, password
, tel
, or url
, this attribute specifies the maximum number of characters that the user can enter.
<span id="min">min</span>
The minimum (numeric or date-time) value for this item, which must not be greater than its maximum (max attribute) value.
##### <span id="multiple">multiple</span>
This Boolean attribute indicates whether the user can enter more than one value. This attribute applies when the type attribute is set to email
or file
, otherwise it is ignored.
<span id="pattern">pattern</span>
A regular expression (RegEx) that the control's value is checked against. The regular expression language is the same as JavaScript RegExp algorithm, with the 'u' parameter that makes it treat the pattern as a sequence of unicode code points. The pattern is not surrounded by forward slashes. In the example below (borrowed from w3schools.com), the input can only be three letter long, no numbers or special characters.
<span id="placeholder">placeholder</span>
A hint to the user of what can be entered in the control . The placeholder text must not contain carriage returns or line-feeds.
Note: Do not use the placeholder
attribute instead of a <label>
element. The <label>
is used to describe the role of the form element, and the placeholder
attribute is a hint about the format that the value should be. There are cases in which the placeholder
attribute is never displayed to the user.
<span id="required">required</span>
This attribute specifies that the user must fill in a value before submitting a form.
<span id="step">step</span>
Works with the min and max attributes to limit the increments at which a numeric or date-time value can be set. It can be the string any or a positive floating point number. If this attribute is not set to any, the control accepts only values at multiples of the step value greater than the minimum.
name
In addition to the above mentioned attributes, all input elements can, and should, take the attribute name
. This attribute is used when sending data with GET or POST, as the name for that input element. E.g.: You will see that radio buttons in the same group will share the same name
. Thus, the data that is sent to the server will be labeled with that name, followed by the value provided by the different radio buttons.
Textarea
The <textarea>
element is a multiline text field. It works like a single line text field, except it allows line break in the typed text by the user. It also accepts three extra attributes to control the rendering across several lines:
Attribute
Default value
Description
cols
20
The visible width of the textarea, in average character widths
rows
The number of visible text lines in the textarea
wrap
soft
Indicates how the texarea wraps text, either hard
or soft
.
Unlike the <input>
element (which is a self closing element) the <textarea>
is a regular element that can contain text content child elements (e.g. <code>
). The <textarea>
element only accepts text as content. Any code or markup (HTML) will be rendered as plain text.
Note that since the <textarea>
element is an element with opening and closing tags, the value is written inside the element, and not as the value to the value
attribute.
Select, option and optgroup
The <select>
element lets you build select boxes (sometimes also known as combo boxes). A select box is a widget that lets a user choose one or more predefined values. Each value inside the select box is defined with an <option>
element and those elements can be grouped inside <optgroup>
elements.
If an <option>
element is set with a value attribute, that attribute's value is sent when the form is submitted. If the value attribute is omitted, the content of the <option>
element is used as the select box's value.
On the <optgroup>
element, the label attribute is displayed before the values, but even if it looks somewhat like an option, it is not selectable.
Last updated
Was this helpful?