The HTML <form> autocomplete attribute allows the browser to automatically fill the form fields based on previous user inputs.
- Used to save time by reusing data like names, emails, and addresses.
- It can be turned "on" or "off" depending on the field's requirements.
Syntax
<form autocomplete="on|off">- on: It has a default value. It specifies that autocomplete is enabled.
- off: It specifies that the autocomplete is disabled.
<!--Driver Code Starts-->
<html>
<body>
<!--Driver Code Ends-->
<form autocomplete="on">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- The autocomplete="on" attribute is used to allow the browser to automatically fill in values for the form fields based on previously entered data.
- When autocomplete is enabled, browsers try to predict and offer values for the name, email, or other fields.
Example of HTML <form> autocomplete Attribute
1. Basic Form with Autocomplete Enabled
<!--Driver Code Starts-->
<html>
<body>
<!--Driver Code Ends-->
<form autocomplete="on">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- The autocomplete="on" attribute enables the browser to suggest previously entered values for the username and email fields.
- This feature enhances user experience by reducing the need to re-enter information.
2. Form with Specific Autocomplete Values
<!--Driver Code Starts-->
<html>
<head>
<style>
form {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"], input[type="email"], input[type="password"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<!--Driver Code Ends-->
<body>
<form autocomplete="on">
<label for="username">Username:</label>
<input type="text" id="username" name="username" autocomplete="username">
<label for="email">Email:</label>
<input type="email" id="email" name="email" autocomplete="email">
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocomplete="new-password">
<input type="submit" value="Register">
</form>
</body>
<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->
- The autocomplete="on" attribute enables autocomplete for the entire form.
- Specific autocomplete values like username, email, and new-password are set on individual input fields to guide the browser in providing appropriate suggestions.
Best Practices for HTML <form> Autocomplete Attribute
- Use autocomplete="on" to enhance user experience by auto-filling known data.
- Set specific autocomplete values (e.g., username, email) for better browser suggestions.
- Disable autocomplete (autocomplete="off") only for sensitive or secure fields like OTPs.