All of us have seen the autocomplete options, while filling the forms online or even logging into our social accounts. All of us fill up so much information online in our day to day life. Mostly used example can be of Gmail or Facebook. When we login to Gmail or Facebook, we enter the username and password. Over the period, have you noticed the username getting displayed just as you type in the few initial letters? This amazing feature is the autocomplete feature.
It is very convenient for us to fill the forms easily, when the autocomplete feature is available. At times, we would like to avoid, using this feature for the security/confidentiality purposes. Suppose, I access my company's Email account from a cyber cafe and I don't want the autocomplete functionality to grab and remember my Email and display it to the other users. HTML5 gives us the power to create the forms, where we can enable and disable this autocomplete functionality just by adding an attribute to our input/form tag.
Autocomplete is an attribute, which can be used with the form tag and input tag. Autocomplete attribute was not present in the previous versions of HTML. It is a new feature of HTML5. It's value can be either "on" or "off".
Syntax
To set autocomplete on/off for the entire form, one can state:
- <form autocomplete="on/off">
To set autocomplete on/off for the specific input elements:
- <input autocomplete="on/off">
In the syntax, given above, value "on" enables the autocomplete and "off" disables the autocomplete.
Let us understand autocomplete by implementing it in a form. Create a simple login form by referring the code snippet, given below:
- <!DOCTYPE html>
- <html>
-
- <body>
- <form> Username: <input type="text" name="user"> <br> <br> Password: <input type="password" name="pwd"> <br> <br> <input type="submit"> </form>
- </body>
-
- </html>
Now, when we we use this form several times, we will see the autocomplete options.
As you can see in the highlighted area, the Browser gives us predictions for autocomplete, as we have not set it off. Autocomplete is enabled by default. Now, let us disable it. Add the highlighted code to our existing form.
- <!DOCTYPE html>
- <html>
-
- <body>
- <form autocomplete="off"> Username: <input type="text" name="user"><br><br> Password: <input type="password" name="pwd"><br><br> <input type="submit"> </form>
- </body>
-
- </html>
Now, you can see the Browser doesn't gives any predictions based on previous records. You can also turn autocomplete off for the entire form and on for few input elements.
Try the code snippet, given below and observe the results.
- <!DOCTYPE html>
- <html>
-
- <body>
- <form autocomplete="on"> First name: <input type="text" name="fname"><br><br> Middle name: <input type="text" name="mname"><br><br> Last name: <input type="text" name="lname"><br><br> Phone: <input type="text" name="phone" autocomplete="off"><br><br> E-mail: <input type="email" name="email" autocomplete="off"><br><br> <input type="submit"> </form>
- </body>
-
- </html>
The code, given above, has 5 input tags. Autocomplete is on for the form but off for the phone and Email input tags. Hence, the predictions won't be given for the phone and Email text fields.
Autocomplete is recommended for the input types, which allows the data entry.