5. Forms
Table of Contents
Q: What is a form and why is a form useful?
What is a Form?
A form is a collection of inputs that can be filled out by a user to submit a collection of data.

Q: What kinds of data might we collect in a form?
Q:When should we use a form? For all user input?
Click a button to pop open a tab with more data - use a
button, not aformA text input that filters the list of data shown - use an
input, but not a fullformA link that takes the user to a new location - use an
anchoror abutton, not aformRegistering a new user with a name, password, and user details - use a
form!
Form Basic Structure
Forms are made up of a few parts:
<form>: the container for the formh2 (or other header): A heading to describe your form<label>: A label for each input<input>: A place for the user to enter some data. There are many types (text, radio, checkbox, etc...)<button>: A button to submit the form
Below is a full example! For now, just focus on the Elements that are in the example (form, h2, label, input, button)
There are a lot of attributes to learn, particularly for label and input. We'll go over them but here are the essential new ones that we'll cover:
formElements have anaria-labelfor accessibilityinputElements have atypeattribute to determine the kind of input (text, number, color, etc...)inputElements have anameattribute which will be used to extract data from the forminputElements MUST have anidattributelabelElements MUST have aforattribute equal to theinputElement'sid. This connects them.
Inputs
Inputs are where users can input their data. Each input Element has a type — the basic inputs are "text" or "number" but there are many more cool ones.
Here is an example (note we're missing labels!)
💡 Best Practice: Use
kabob-caseforidandcamelCaseforname(we'll learn why in a moment)
Some other types of inputs Elements (other than the literal input tag) are the select and textarea (and technically buttons).
Inputs must have a name attribute. We'll use those later when we are getting data from the form when it is submitted.
Accessibility: Labels and aria-label
Right now, our form is just a bunch of inputs. But how does the user know which input is for which value? Well, for one, we could add a header:
But that only helps our seeing users! When designing websites, we must design for ALL of our users.
Labels are crucial for our non-seeing users and accessibility. They tell screen readers what the purpose of an element is. There are two kinds of labels that we'll use:
The
aria-label/aria-labelledbyattribute — describes the purpose of aform(or really of any element).The
<label>element — describes the purpose of an<input>element.
A few notes about aria-lablledby
Accessible Rich Internet Applications (ARIA) is a set of roles and attributes that define ways to make web content and web applications more accessible to people with disabilities.
We added an
idto theh2elementWe added the
aria-lablledbyattribute to theformelement so that screen readers know to use theh2element text as the aria label.
Here are a few notes about the <label> element:
Connect the
<label>to the<input>with theforattribute. It should be the same as the input'sid:Labels make it so that clicking a label will focus the input or check the checkbox.
Labels help our seeing users too by describing the input!
Notice that we also wrap each label + input pair inside of a div. This isn't necessary but it makes styling each pair of elements a lot easier.
Submit button
All forms should end with a submit button.
By default button elements have a type="submit" so you don't need to put it. But you can if you want! There are other types of buttons we'll discuss later.
Clicking the submit button (or pressing enter while focused inside a form) will fire the 'submit' event on the form.
Our objective is to...
listen for this
'submit'eventcreate an event handler that extracts the user input data
do something with that data! (save it in a database, show it in the UI, etc...)
Input types
On the index.html go over the basic text inputs, but then also radio groups (with fieldsets and legends), and the select. Point out when to use them:
<input type="text">: any open ended but short data<input type="number">: any number, min and max can lock you in (not featured on form, just mention)<input type="radio">: when you want to pick one of a few options<textarea>: any open ended but long data<select>and<option>: when you want to pick one of a lot of options
HTML Form Validation
required
min
max
pattern
Styling Forms
Vertical form layouts
Horizontal form layouts (multi-column)
Button groups
Responsive Forms
Last updated