Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Global Attributes HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Details HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Character Sets HTML Languages HTML URL Encoding HTML Performance HTML SEO

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Form Validation HTML Progress and Meter HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Advanced

HTML and the DOM Web Components HTML Security Advanced Forms

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Advanced Forms


Share

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
nameThe full name
emailAn email address
telA phone number
street-addressThe street address
postal-codeThe postal code
country-nameThe name of the country
current-passwordThe password the user already has
new-passwordA 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

  • autocomplete helps the browser fill in the form for the user
  • inputmode and enterkeyhint show the right keyboard on phones
  • <output> shows the result of a calculation
  • <fieldset disabled> disables all fields inside it
  • FormData reads the form data with JavaScript

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.