Skip to content

Instantly share code, notes, and snippets.

@eduardo-mior
Last active April 1, 2024 15:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eduardo-mior/ad14cab17fb5ab641812bff622534353 to your computer and use it in GitHub Desktop.
Save eduardo-mior/ad14cab17fb5ab641812bff622534353 to your computer and use it in GitHub Desktop.
How to disable HTML Input Autocomplete

Definitive guide → 6 ways to solve

I would very much like there to be a WEB standard for this, but unfortunately there is not!

I studied this for several days and gathered a lot of information and I will share it with you.

Before developing or using an internet hack you need to know that there are 2 types of Autocomplete. There is the autocomplete of the page and the autocomplete of the browser.

This is the global browser autocomplete. It appears whenever you define autocomplete="off" or when you define no autocomplete but define type="email" for example. The global autocomplete suggests emails you've used on other sites. The global autocomplete has a manage button at the bottom and can be disabled in the browser config. image

The page's autocomplete is this. It appears when you define a specific autocomplete, for example autocomplete="customer-name". This autocomplete never appears right away, it starts to appear when you start filling out the same form many times. For example, you have a screen where you register your customers, after the fourth or fifth registered customer Chrome will start giving suggestions with the names of the customers already registered. The page autocomplete does not have a manage button at the bottom, unlike the global autocomplete. image

There are several Hacks to work around this problem. You must choose the one that best fits your project. Below is a brief list of the Hacks that can be used and that work:

  • Add the readonly property to the input and remove the property when the input is clicked or when it receives focus so that the user can type (not tested by me). Example:
 <input type="password" readonly onfocus="this.removeAttribute('readonly');"/>

 

  • Set the input type to search type="search" and turn autocomplete off autocomplete="off" (not tested by me). Example:
<input type="search" autocomplete="off"> 

 

  • Place a div around the input and before the input place an input with display: none and tabindex"-1" (not tested by me). Example:
<div>
    <input class="hidden" tabindex="-1" aria-hidden="true">
    
    <label for="email">Email</label>
    <input id="email" name="email" type="email" autocomplete="off">
</div>

 

  • Some people also reported that if you put a field exactly the same on top, however with a different id and display none will also work (not tested by me). Example:
<input type="email" id="fake-mail" name="email" class="hidden"> 
<input type="email" id="real-email" name="email">

 

  • Define a random name and an autocomplete for the input every time the page is reloaded. In this case you would always use the ID attribute to work (this was the solution that I implemented in my project). It worked perfectly for me. Example:
<input id="email" name="af2Km9q" autocomplete="x7pv66b">

 

  • Some people reported that put some non-printable characters on label and input placeholder also works. According to some reports Briggers take into account input ID, Name, Label and Placeholder, and placing some magical characters do not print in random places of Placeholder and Label, we cheat the browser (not tested by me). Example:
<label for="email">Your&#8204; Ema&#8204;il</label> 
<input type="email" id="email" name="email" placeholder="Type&#8204; yo&#8204;ur ema&#8204;il">

 

  • A workaround that also works is to use a <div> tag with the contenteditable="true" property instead of using the <input> tag. The contenteditable="true" property allows users to edit the content of the element, in which case they will edit the content of the div which is our fake input. This solution is adopted by several giant companies in the market, such as Facebook and Microsfot for example. Example:
<div contenteditable="true"></div>

 
Some other considerations:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment