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.

AttributeDescriptionRequired
typetype of the inputno (defaults to text)
namename of the inputyes (is submitted with the form as a name/value pair)
idid of the inputno (useful when using labels)
placeholdercontent to appear when input is emptyno (it is often better to use labels instead)
valuevalue of the inputno
disabledwhether the input should be disabledno

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)

AttributeDescriptionRequired
checkedwhether checkbox should be toggled on by defaultno (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)

AttributeDescriptionRequired
maxlatest acceptable dateno
minearliest acceptable dateno

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)

AttributeDescriptionRequired
acceptwhat kind of file types to acceptno
multiplewhether to allow selection of multiple filesno (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)

AttributeDescriptionRequired
maxmaximum acceptable numberno
minminimum acceptable numberno
stepstepping interval when using the built-in arrows to adjust valueno

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)

AttributeDescriptionRequired
maxlengthmaximum length of passwordno
minlengthminimum length of passwordno

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:
AttributeDescriptionRequired
checkedwhether the radio is checked/selectedno
valuethe value submitted when form is submitted paired with the nameno (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)

AttributeDescriptionRequired
maxlengthmaximum length of textno
minlengthminimum length of textno

Read more on MDN

<input type="text"

Test yourself

Description

Create a user registration form by

  1. Adding a text input element for the name
  2. Adding a password input element for the password
  3. Adding a file input 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. 1 You should create the name text input element.
  2. 2 Your name text input element should have the id 'name'.
  3. 3 Your name text input element should have the name 'name'.
  4. 4 Your name text input element should be required.
  5. 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. 6 You should create the password input element.
  7. 7 Your password input element should have the id 'password'.
  8. 8 Your password input element should have the name 'password'.
  9. 9 Your password input element should be required.
  10. 10 Your password input element should have a non-empty label with a 'for' attribute matching the 'id' attribute in the accompanying input.
  11. 11 You should create the profile picture file input element.
  12. 12 Your profile picture file input element should have the id 'picture'.
  13. 13 Your profile picture file input element should have the name 'picture'.
  14. 14 Your profile picture file input element should not be required.
  15. 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.
Editor
Preview