HTML Advanced Forms
Better Forms
You already know how to make a form with labels, input fields, and a submit button.
This chapter shows some simple ways to make your forms easier and faster to use.
If you are new to forms, start with the HTML Forms chapter.
Help the Browser Fill In Forms
Browsers can save information like your name, email, and phone number.
The autocomplete attribute tells the browser what each field is for.
The browser can then offer to fill in the fields for the user:
Example
<form action="/action_page.php">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" autocomplete="name"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" autocomplete="email"><br><br>
<label for="phone">Phone:</label><br>
<input type="tel" id="phone" name="phone" autocomplete="tel"><br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself »
Here are some common values:
| Value | The field is for |
|---|---|
| name | The full name |
| An email address | |
| tel | A phone number |
| street-address | The street address |
| postal-code | The postal code |
| country-name | The name of the country |
| current-password | The password the user already has |
| new-password | A new password, for example when the user signs up |
To learn how to turn autocomplete on or off, read the HTML Input Attributes chapter.
Show the Right Keyboard on Phones
On a phone, the keyboard can change to fit the field.
The inputmode attribute tells the phone which keyboard to show.
The enterkeyhint attribute tells the phone what to write on the Enter key:
Example
<label for="code">Code from the letter:</label><br>
<input type="text" id="code" name="code" inputmode="numeric"><br><br>
<label for="search">Search:</label><br>
<input type="text" id="search" name="search" enterkeyhint="search">
Try it Yourself »
On a phone, the first field shows a number keyboard, and the second field shows a Search key.
On a computer, the fields look and work like normal text fields.
inputmode is useful for codes and numbers that are not real numbers, like a code from a letter.
Tip: Fields with type="email" and type="tel" already show the right keyboard.
Show a Result with output
The <output> element shows the result of a calculation.
In this example, the result changes each time the user changes a number:
Example
<form oninput="result.value = Number(a.value) + Number(b.value)">
<input type="number" id="a" name="a" value="5"> +
<input type="number" id="b" name="b" value="3"> =
<output name="result" for="a b">8</output>
</form>
Try it Yourself »
The page first shows 5 + 3 = 8.
If you change 5 to 10, the result changes to 13.
oninput runs each time a field in the form changes.
Number() turns the text in the fields into numbers, so they are added and not joined.
The for attribute lists the ids of the fields that the result comes from.
Disable a Group of Fields
The <fieldset> element groups fields together.
If you add disabled to a fieldset, all fields inside it are disabled.
In this example, the gift card fields are only enabled when the user checks the "This is a gift" box:
Example
<input type="checkbox" id="gift" onchange="document.getElementById('giftFields').disabled = !this.checked">
<label for="gift">This is a gift</label><br><br>
<fieldset id="giftFields" disabled>
<legend>Gift card</legend>
<label for="to">To:</label><br>
<input type="text" id="to" name="to"><br><br>
<label for="message">Message:</label><br>
<input type="text" id="message" name="message">
</fieldset>
Try it Yourself »
When the page loads, you cannot type in the To and Message fields.
When you check "This is a gift", both fields are enabled.
!this.checked means "not checked", so the fieldset is disabled when the box is not checked.
Note: Disabled fields are not sent with the form.
Read Form Data with JavaScript
Sometimes you want to use the form data on the page, without sending it to a server.
new FormData() collects all fields in a form, and get() reads one field by its name:
Example
<form id="myForm" onsubmit="showData(event)">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="Maria"><br><br>
<label for="city">City:</label><br>
<input type="text" id="city" name="city" value="Oslo"><br><br>
<input type="submit" value="Submit">
</form>
<p id="result"></p>
<script>
function showData(event) {
event.preventDefault();
const data = new FormData(document.getElementById("myForm"));
document.getElementById("result").textContent =
"Hello " + data.get("fname") + " from " + data.get("city") + "!";
}
</script>
Try it Yourself »
When you click Submit, the page shows:
Hello Maria from Oslo!
event.preventDefault() stops the form from being sent, so the page does not reload.
get() uses the name attribute of the field, not the id.
The example uses textContent, because the text comes from the user (see HTML Security).
Summary
autocompletehelps the browser fill in the form for the userinputmodeandenterkeyhintshow the right keyboard on phones<output>shows the result of a calculation<fieldset disabled>disables all fields inside itFormDatareads the form data with JavaScript