Additional features in forms
Modern HTML forms can do far more than plain text boxes. Specialised input types, suggestion lists, autofill hints and the FormData object let you build forms that are quicker to fill in, work better on phones, and are easy to read from JavaScript. These features build on basic validation and need little or no extra code.
Input types that help users
Choosing the right type changes both the browser widget and the mobile keyboard:
| Type | What the user gets |
|---|---|
email | Keyboard with "@", basic format checking |
tel | Numeric phone keypad on mobile |
number | Spinner buttons, honours min, max and step |
date, time | Native date and time pickers |
range | A slider between min and max |
color | A colour picker returning a hex value |
search | A text field that may show a clear button |
Browsers that do not support a type simply fall back to a normal text field, so using them is safe.
Suggestions, hints and focus
A datalist attaches a list of suggestions to a text input through the list attribute. Users can pick one or type something else. The autocomplete attribute tells the browser which saved data fits the field, such as name, email, tel, new-password or postal-code, so autofill fills the right boxes. autofocus places the cursor in a field when the page loads; use it only once per page, because jumping focus can confuse screen reader users.
Reading a whole form with FormData
Instead of reading each input one by one, pass the form element to new FormData(). It collects every field that has a name attribute. You can loop over it, read single values with get, or turn it into a plain object.
<form id="booking">
<input name="city" list="cities" autocomplete="address-level2">
<datalist id="cities">
<option value="Delhi"></option>
<option value="Mumbai"></option>
<option value="Bengaluru"></option>
</datalist>
<input name="guests" type="number" min="1" max="8" value="2">
<input name="checkin" type="date">
<input name="ref" type="hidden" value="home-page">
<button>Book</button>
</form>
<script>
const form = document.getElementById("booking");
form.addEventListener("submit", (event) => {
event.preventDefault();
const data = new FormData(form);
const booking = Object.fromEntries(data);
console.log(booking); // { city: "...", guests: "2", checkin: "...", ref: "home-page" }
});
</script>
Notice that guests comes back as the string "2". Every value from a form is text, so convert numbers with Number() or read input.valueAsNumber directly.
Disabled versus readonly
Both stop the user from editing, but they behave differently. A readonly field can still be focused and is included when the form is submitted. A disabled field is greyed out, skipped by the Tab key, and its value is not submitted or included in FormData. Developers often disable a field to "lock" it and then wonder why the server never received the value. If you need the value sent, use readonly or a hidden input.
Grouping and resetting
A fieldset with a legend groups related fields, such as a set of radio buttons, and gives screen readers a heading for the group. Setting disabled on the fieldset disables every control inside it at once. A button with type="reset", or calling form.reset(), returns all fields to their initial values.
Takeaway: pick input types that match the data, add datalist and autocomplete hints, read the form with FormData, and remember that disabled fields are never submitted.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.