Every HTML form elements is covered in this chapter.
The HTML < form > Elements
The HTML <form> element can contain one or more of the following form elements:
<input>
<label>
<select>
<textarea>
<button>
<fieldset>
<legend>
<datalist>
<output>
<option>
<optgroup>
The < input > Element
The <input> element is one of the most often used form elements.
There are multiple methods to show the <input> element based on the typeattribute.
Example
The < label > Element
A label for multiple form elements is defined by the <label> element.
Users of screen readers will find the <label> element helpful since it will read the label aloud when the user focuses on the input element.
The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) – because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.
To link them together, the id attribute of the <input> element and the forattribute of the <label> tag must be equivalent.
The < select > Element
The <select> element defines a drop-down list:
Example
A selectable option is defined via the <option> element.
The first item in the drop-down list is picked by default.
Add the chosen attribute to the option in order to define a pre-selected option:
Example
Visible Values:
Use the size attribute to specify the number of visible values:
Example
Allow Multiple Selections:
Use the multipleattribute to allow the user to select more than one value:
Example
The < textarea > Element
The <textarea> element defines a multi-line input field (a text area):
Example
The number of visible lines in a text area is specified by the rowsattribute.
The visible width of a text section is specified by the colsattribute.
The HTML code above will appear in a browser like this:
You can also define the size of the text area by using CSS:
Example
The < button > Element
The <button> element defines a clickable button:
Example
This is how the HTML code above will be displayed in a browser:
The < fieldset > and < legend > Elements
The <fieldset> element is used to group related data in a form.
The <legend> element defines a caption for the <fieldset> element.
Example
This is how the HTML code above will be displayed in a browser:
The < datalist > Element
A list of pre-defined options for a <input> element is specified via the <datalist> element.
When entering data, users will get a drop-down list of the pre-defined alternatives.
The id property of the <datalist> element must be referred to via the list attribute of the <input> element.
Example
The < output > Element
The outcome of a calculation (such as one carried out by a script) is represented by the <output> element.
Example
Perform a calculation and show the result in an <output>element:
HTML Form Elements
Tag
Description
<form>
Defines an HTML form for user input
<input>
Defines an input control
<textarea>
Defines a multiline input control (text area)
<label>
Defines a label for an <input> element
<fieldset>
Groups related elements in a form
<legend>
Defines a caption for a <fieldset> element
<select>
Defines a drop-down list
<optgroup>
Defines a group of related options in a drop-down list
<option>
Defines an option in a drop-down list
<button>
Defines a clickable button
<datalist>
Specifies a list of pre-defined options for input controls