HTML Elements
Inputs
HTML user <input>s are used to create interactive elements in
web-based forms. Their job is to accept data from the user by using
various types of built-in types. We have over 20 different input types
at our disposal, which gives us a lot of options, but also at the same
time makes <input> one of the most complex elements to use correctly.
We usually use <input> elements inside a <form>, which makes it
possible to validate input data client side.
Common attributes
Every type of <input> has their own set of attributes, but they do
share some commonly used attributes.
| Attribute | Description | Required |
|---|---|---|
| type | type of the input | no (defaults to text) |
| name | name of the input | yes (is submitted with the form as a name/value pair) |
| id | id of the input | no (useful when using labels) |
| placeholder | content to appear when input is empty | no (it is often better to use labels instead) |
| value | value of the input | no |
| disabled | whether the input should be disabled | no |
Read more on MDN
<input>: The Input (Form Input) element
Labels
The HTML <label> element should be used to describe each and every
visible <input>. The for attribute specifies which <input> the
label is describing, which requires the <input> to have a matching
id attribute.
An alternative is to nest the input element inside the label, which
makes the association between <input> and <label> implicit. What
method we use, it mostly a matter of taste, but we should try to be
consistent once we have made our choice.
Labels help people who use assistive technologies because they can tell the user what a particular user input is meant for.
Code example
<label for="age">Age (years):</label><input type="number" name="age" id="age" required>
which produces (give some styling)
Read more on MDN
<label>
Common types
Checkbox
Checkboxes should be used to toggle an option on or off. Toggling it on is usually considered a positive action and should be reflected as a positive/true value on the state.
<label for="displayEmptyColumns">Display empty columns:</label><input type="checkbox" name="displayEmptyColumns" id="displayEmptyColumns">
which produces (give some styling)
| Attribute | Description | Required |
|---|---|---|
| checked | whether checkbox should be toggled on by default | no (defaults to false) |
Read more on MDN
<input type="checkbox"
Date
Date <input>s are used to create input fields where the user can
enter a date, either by picking it from a native browser date picker
or typing it in. The browser will automatically validate the input and
check that a valid date has been entered
<label for="startDate">Start date:</label><input type="date" name="startDate" id="startDate">
which produces (give some styling)
| Attribute | Description | Required |
|---|---|---|
| max | latest acceptable date | no |
| min | earliest acceptable date | no |
Read more on MDN
<input type="date"
File
The file input type is used for file selection. These files can then
be uploaded to a server when the form is submitted, but can also be
passed to JavaScript functions for custom handling.
<label for="license">License:</label><input type="file" name="license" id="license"accept="image/png, image/jpeg">
which produces (give some styling)
| Attribute | Description | Required |
|---|---|---|
| accept | what kind of file types to accept | no |
| multiple | whether to allow selection of multiple files | no (defaults to false) |
Read more on MDN
<input type="file"
Number
The number input type allows users to enter a number. The browser
will automatically check that a valid number has been entered.
<label for="sabers">Sabers (1-4):</label><input type="sabers" name="sabers" id="sabers"min="1" max="4" value="1" required>
which produces (give some styling)
| Attribute | Description | Required |
|---|---|---|
| max | maximum acceptable number | no |
| min | minimum acceptable number | no |
| step | stepping interval when using the built-in arrows to adjust value | no |
Read more on MDN
<input type="number"
Password
The password input type is a text field where user input is visually
obscured such that it cannot easily be read. It is important to
remember that the value stored is not safe or encrypted in any
way. Password fields are mostly used to hide the password from curious
people sitting nearby.
<label for="password">Password (8-64 characters):</label><input type="password" name="password" id="password"minlength="8" maxlength="64" required>
which produces (give some styling)
| Attribute | Description | Required |
|---|---|---|
| maxlength | maximum length of password | no |
| minlength | minimum length of password | no |
Read more on MDN
<input type="password"
Radio
We can use radio input type elements when we want to allow the user
to select a single option from a choice of several. We group radio
buttons together by giving them the same name. The browser will then
ensure that only a single radio is selected from the radio group.
We should avoid using radio buttons when only a single option is available, and instead use checkboxes.
<div>Please select preferred shipping method:<label><input type="radio" name="shippingMethod"value="dhl" checked>DHL</label><label><input type="radio" name="shippingMethod"value="singapore-post">Singapore Post</label></div>
which produces (give some styling)
Please select preferred shipping method:
| Attribute | Description | Required |
|---|---|---|
| checked | whether the radio is checked/selected | no |
| value | the value submitted when form is submitted paired with the name | no (defaults to 'on') |
Read more on MDN
<input type="radio"
Text
The text is the default input type and is in many ways the most
generic type. It provides almost no browser validation, except for
length checks and limits. We use the text type when the user input
data cannot be limited to one of the other types.
<label for="name">Name (1-128 characters):</label><input type="text" name="name" id="name"minlength="1" maxlength="128" required>
which produces (give some styling)
| Attribute | Description | Required |
|---|---|---|
| maxlength | maximum length of text | no |
| minlength | minimum length of text | no |
Read more on MDN
<input type="text"
Test yourself
Description
Create a user registration form by
- Adding a
textinput element for the name - Adding a
passwordinput element for the password - Adding a
fileinput element for the profile picture
as the elements of the <form>. All inputs must have an accompanying
label connected to their respective inputs through matched for/id
pairs.
The name and password fields should be required, while the profile picture file field should be optional.
Tests
- 1 You should create the name text input element.
- 2 Your name text input element should have the id 'name'.
- 3 Your name text input element should have the name 'name'.
- 4 Your name text input element should be required.
- 5 Your name text input element should have a non-empty label with a 'for' attribute matching the 'id' attribute in the accompanying input.
- 6 You should create the password input element.
- 7 Your password input element should have the id 'password'.
- 8 Your password input element should have the name 'password'.
- 9 Your password input element should be required.
- 10 Your password input element should have a non-empty label with a 'for' attribute matching the 'id' attribute in the accompanying input.
- 11 You should create the profile picture file input element.
- 12 Your profile picture file input element should have the id 'picture'.
- 13 Your profile picture file input element should have the name 'picture'.
- 14 Your profile picture file input element should not be required.
- 15 Your profile picture file input element should have a non-empty label with a 'for' attribute matching the 'id' attribute in the accompanying input.