Glossary - HTML Form Elements
Glossary - HTML Form Elements
The <form> element in HTML is an important and useful element. The following sheet provides an
overview of the <form> constituent elements and their common attributes with simple examples
for quick reference.
<input>
It is used to create interactive controls, for example, buttons and various types of text fields and so
on, to accept input or data from the user. The key attribute of this element is type. Some common
values for the type include: button, checkbox, date, email, number, password, submit, text,
and url. These values dictate the appearance of the element. For example, this code:
<form action="my_action_page">
<label for="uname">Username:</label>
<br>
<br>
<label for="pwd">Password:</label>
<br>
<br><br>
</form>
<label>
Defines a label for an element. It has an attribute "for", the value of which should be equal to the id
attribute of the element it is associated with. Note how in the example above, the <label> is
associated with the <input> using its id value.
<select>
Defines a drop-down list of options presented to the user. It has a couple of attributes:
Multiple Boolean attribute, when specified, indicates if a user can select multiple options
out of the list
Required indicates if the user is required to select an option before submitting a form
The options in a drop-down list are defined using the <option> element inside <select>. Note the
example in the <option> description below.
<textarea>
Defines a multi-line input field, typically to allow the user to input longer textual data. The common
attributes for this element include:
cols defines the width of the text area, the default value is 20
minlength the minimum number of characters that the user should enter
rows defines the number of visible text lines for the text area
The following line of code defines a text area of 10 visible lines and nearly 30 characters wide where
the user can input a maximum of 200 characters:
</textarea>
<button>
Defines a clickable button. The onclick attribute defines the behavior when the button is clicked by
the user. For example, in the code below, an alert message is shown to the user.
</button>
<fieldset>
Used to group related input elements in a form. For instance, elements related to the user’s
personal information and educational qualification can be grouped separately in two field sets.
<legend>
5
6
10
11
12
13
14
15
<fieldset>
<legend>Personal Info</legend>
</fieldset>
<fieldset>
<legend>Qualificaiton</legend>
</fieldset>
<datalist>
Specifies a list of pre-defined options for an input element. It differs from <select> since the user
can still provide textual or numeric input other than the listed options.
1
2
10
11
12
<form action="/my_action_page">
<datalist id="flowers">
<option value="Rose">
<option value="Lily">
<option value="Tulip">
<option value="Daffodil">
<option value="Orchid">
</datalist>
...
</form>
<output>
Represents the result of a calculation (typically the output of a script) or the outcome of the user
action.
<option>
Defines an option for the drop-down list. The following code example demonstrates how a simple
list can be defined, with the rendered view below the code block.
<option value="js">JavaScript</option>
</select>
By default, the first item in the drop-down list is selected. To define a pre-selected option, add the
selected attribute to the option.
<optgroup>
Defines a group of related options in a drop-down list. Its attribute label names the group.