Chrome Autofill covers Autocomplete for Google Maps API v3

HtmlGoogle MapsGoogle ChromeGoogle Maps-Api-3

Html Problem Overview


I am using Google Maps Javascript v3 to setup autocomplete on an HTML input field like so:

http://imgur.com/Rm6X2FI.png - (w/out autofill)

The issue I'm having is that Chrome's Autofill covers up the Maps Autocomplete like this:

http://imgur.com/7a2QM7L.png - (w/ autofill)

I found a solution online a couple of months ago (ref: https://stackoverflow.com/questions/15738259/disabling-chrome-autofill), but it seems that this solution no longer has any effect.

<input type="text" name="address" id="address_fake" autocomplete="off" style="display:none">
<input type="text" name="address" id="address" autocomplete="off" placeholder="Address" />

Is there any way I can stop Chrome's Autofill from showing on top of the Google Maps Autocomplete list?

EDIT: https://stackoverflow.com/questions/18531437/stop-google-chrome-auto-fill-the-input does not provide a solution to my current issue. The issue is with the Chrome Autofill dropdown on the input field, not the input field being automatically filled with an autofill value.

Html Solutions


Solution 1 - Html

None of the above answers worked for me (Chrome 64.0.3282.186, x64 windows).

TL;DR: The Google Maps code is changing the autocomplete attribute to off, so even if you set it to new-password, you'll still get autofill. The hack I'm using is to listen for mutations on that attribute and then override it. Note: simply changing the attribute after calling into Google Maps does not work.

Set up a MutationObserver before initializing Google Maps Autocomplete that immediately stops listening for mutations and then sets the attribute to new-password.

    var autocompleteInput = document.getElementById("id-of-element");

    var observerHack = new MutationObserver(function() {
        observerHack.disconnect();
        $("#id-of-element").attr("autocomplete", "new-password");
    });

    observerHack.observe(autocompleteInput, {
        attributes: true,
        attributeFilter: ['autocomplete']
    });

Solution 2 - Html

This was driving me totally crazy as well. Have the same issue. We use scripting to pull up field values for a user to select from and DO NOT want the browser's auto-whatever to mess this up. Chrome seems to be the bugger (latest version 42.0.2311.135 m), Firefox (FF) we can work with.

So, we have to deal with the browser's autocomplete AND Chrome's autofill as well. If I add: <form autocomplete="off"> at the top then it stops the autocomplete in FF and Chrome but not the AUTOFILL in Chrome. Changing 'off' to 'false' does nothing for either browser. Solved the FF issue but not Chrome as it shows the ugly AUTOFILL box over content.

If you add autocomplete="off" to each of the fields individually then again, works in FF but for the input fields that have this attribute autocomplete in Chrome is off but the autofill still rears its ugly head.

Now, the odd thing is that if you change the value in the individual input field from "off" to "false" then it seems to shake Chrome up and for the field you have this set to autocomplete="false" then you ONLY see the autocomplete values (if anything was entered in the field before) and all the other input fields show nothing! You can also set this value to no or xx or whatever and seems like Chrome barfs on the invalid value for autocomplete and the form reacts strangely. If you have 5 fields and set this for the third field then fields 1,2, 4 and 5 are blank but field 3 shows autocomplete.

This is an example for you to copy and mess with (try moving the autocomplete attribute to different fields and see how it reacts) :

<!DOCTYPE html>
<html>

<head>
  <title>Signup</title>
</head>

<body>
  <form autocomplete="off" method="post">
    First name:
    <input name="Firstname" type="text">
    <br />Last name:
    <input name="Lastname" type="text" style="width: 124px">
    <br />Address:
    <input autocomplete="false" name="Address" type="text" style="width: 383px">
    <br />Phone number:
    <input name="Phone" type="text">
    <br />E-mail:
    <input name="Email" type="text" style="width: 151px">
    <br />
    <input type="submit">
  </form>
</body>

</html>

My solution to turn off both autocomplete and Chrome's autofill (you should be able to put the hidden input field at the top or bottom below the submit). Add this <input autocomplete="false" name="hidden" type="text" style="display:none;"> to the code:

<!DOCTYPE html>
<html>

<head>
  <title>Signup</title>
</head>

<body>
  <form autocomplete="off" method="post">
    <input autocomplete="false" name="hidden" type="text" style="display:none;">
    <br />First name:
    <input name="Firstname" type="text">
    <br />Last name:
    <input name="Lastname" type="text" style="width: 124px">
    <br />Address:
    <input name="Address" type="text" style="width: 383px">
    <br />Phone number:
    <input name="Phone" type="text">
    <br />E-mail:
    <input name="Email" type="text" style="width: 151px">
    <br />
    <input type="submit">
  </form>
</body>

</html>

Bottom line: Chrome does adhere to the autocomplete=off but the autofill of Chrome is the problem. Hope this helps.

Solution 3 - Html

Thanks to GSTAR. Instead of Jquery, I used the below code & this worked for me

<input type="text" value="" id="address" class="form-control" placeholder="Enter a location" autocomplete="new-password" onfocus="this.setAttribute('autocomplete', 'new-password');">

Solution 4 - Html

jQuery solution:

$('#address').focus(function() {
    $(this).attr('autocomplete', 'new-password');
});

Solution 5 - Html

via Google Maps Event w/ES6...

new google.maps.places.Autocomplete(input);
google.maps.event.addDomListener(input, 'focus', e => e.target.setAttribute('autocomplete', 'new-password'));

Solution 6 - Html

You can try changing the type attribute of your input field to search instead of text.

This will not allow the autofill suggestions to open up on that particular input field.

change this <input type="text" .... /> to <input type="search" ... />

Have tested on chrome 83 and safari 13.0.1. Works fine for me.

Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search

Solution 7 - Html

To quote Elmux's comment on answer https://stackoverflow.com/a/50927328/1350573 above:

> With Chrome 71, I removed the attribute placeholder from the console, > and it works automagically. If I change the attribute value for > something else, it works too. Finally, avoid using the term 'Address' > in the placeholder attribute. – Elmux

For me (30th December 2018, Chrome 71) this was the correct, but non-intuitive answer.

Solution 8 - Html

Browser - Chrome.

For input where google api is attached, set attribute autocomplete on focus -

onfocus="this.setAttribute('autocomplete', 'new-password')"

and for other fields setting this attribute directly works -

autocomplete="new-password"

For me only this value worked. Any other value and the stupid autofill makes a mess.

Solution 9 - Html

I added a EventListener for focus setting the autocomplete attribute. Worked for me.

const initAutocomplete = () => {
    address = document.getElementById('GPA');
    city = document.getElementById('city');
    zip = document.getElementById('zip');
    autocomplete= new google.maps.places.Autocomplete(address, {
    componentRestrictions: { country: ["de"] },
    fields: ["address_components"],
    types: ["address"],
    });
    
    autocomplete.addListener("place_changed", fillInAddress);
    
    address.addEventListener('focus', () => {
      address.setAttribute('autocomplete', 'new-password')
    })
  }

Solution 10 - Html

Work on Chrome v 90: Put the following in your

onfocus="$(this).attr('autocomplete', 'no')"

Solution 11 - Html

Use autocomplete="new-password" for input field for which you want auto-fill to be disabled. Google Chrome intentionally ignores autocomplete="off | false" but if you set to "new-password", then it ignores auto-fill.

Also, check Google Places API way of handling auto-fill, ideally, this should be the approach for implementing: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

Solution 12 - Html

For me, I inserted a string at run time in the middle of the input box ID and form field name that was replaced with the session ID of the current users session. The session ID is as close as anyone can get to unique for every user and session.

In short, what was previously a field called "cust_address" became "cust_a734ohljehgto87y34hpwy9pho3ghseddress" as did the ID of the field My input text box code looked like this:

'<input type=text name="cust_a%sessionid%ddress" id="cust_a%sessionid%ddress" value="" autocomplete="new-password">'

For extra precautions I added the autocomplete="new-password" which seems to work for other fields just not the address allthough it does seem to work on occasion and I havent worked out what causes it to fail or work (yet)...

When my user requests the form, I load it into a variable in PHP and replace all instances of %sessionid% with the session ID of the current user session then output it to the browser.

So far, this has worked like a charm but my system is still in beta testing so it has not had thousands of inputs monitored by Chrome in order for Chrome to figure out the logic and circumvent it - hopefully they dont spend too much time on circumventing these fixes especially considering it is GOOGLES OWN SYSTEM that is overriding GOOGLES OWN SYSTEM with autocomplete overriding auto-fill from calendar places API !

Solution 13 - Html

I had username and password fields on the same page.

Put a simple unused <form></form> around those and this cleared up the Autocomplete problem.

Solution 14 - Html

I had the same issue and solved it using the below jquery function

$( document ).ready(function() {
  if($('#address').length > 0){
    $('#address').on('focus',function() {
        $(this).attr('autocomplete', 'nope');
    });
    
  }
});

Solution 15 - Html

This is the only solution that worked for me with both Autocomplete and Chrome's Autofill: It works also after calling new this.props.google.maps.places.Autocomplete

  • Add autocomplete="off" on the form tag.

  • Set autocomplete="none" directly on the input inside the form and set the attribute again on focus.

<form autocomplete="off">
    <input type="text" name="address" id="address" placeholder="Address" autocomplete="none"
        onfocus="this.setAttribute('autocomplete', 'none');"/>
</form>

Solution 16 - Html

For those that are using Angularjs, I created a directive based on @Rimmel answer that is working for me in Chrome 66.0.3359.139. Below is the code:

(function(){
  'use strict'; 
  angular.module('app.directive')
    .directive('stopAutofill', stopAutofillDirective);

  function stopAutofillDirective() {
      return {
        restrict: 'A',
        link: function (scope, element) {
            var observerHack = new MutationObserver(function () {
                observerHack.disconnect();
                element.attr("autocomplete", "new-password");
            });

            observerHack.observe(element[0], {
                attributes: true,
                attributeFilter: ['autocomplete']
            });
        }
      };
    };
})();

Solution 17 - Html

I tried the sample from Google's Address Finder, and got the same problem:

enter image description here

Updated Answer

With the new version of chrome, all you need is to add: autocomplete="off" to your input:

<input id="autocomplete" autocomplete="off" placeholder="Enter your address" onFocus="geolocate()" type="text"/>

Original Answer - Workaround

This is the automcomplete input, which has the event: onFocus="geolocate()":

<input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text"/>

I added this line to geolocate, to hide the autocomplete:

function geolocate() {
    // add this line to hide the autocomplete
    $("#autocomplete").attr("autocomplete", "new-password");

    // other stuff
}

Solution 18 - Html

Workaround:

$('#Name').on('mouseup keyup', function () {
        var val = $('#Name').val();
        val = val.length;
        if (val === 0) {
            $('#Name').attr('autocomplete', 'on');
        }
        else {
            $('#Name').attr('autocomplete', 'new-password');
        }
    }).on('mousedown keydown', function () {
        var val = $('#Name').val();
        var length = val.length;
        if (!length) {
            $('#Name').attr('autocomplete', 'new-password');
        }
    })

Solution 19 - Html

I managed to fix this issue for Chrome 69 by changing the input's placeholder.

I had 2 inputs tied to Google Places Autocomplete. As soon as you clicked the field, autofill showed up covering the suggestions from Autocomplete. I also had a listener set-up to change the 'autocomplete' attribute of the input to something random, like 'no-google-autofill'. That worked well until I upgraded to Chrome 69.

Eventually, I fixed it by changing the input's placeholder from 'Postcode' to 'Area'. Apparently, the autofill got triggered because of the placeholder. Sometime's Chrome tries to be too smart for its own good.

Solution 20 - Html

This is driving me crazy, after testing lot of stuff, what I had to do to prevent this behavior is not giving a hint to chrome that's an address.

Not an easy challenge to make it user friendly without mentioning the words "address", "street", etc

I end up using the word "Location" in the placeholder. I didn't use any other attribute like autocomplete. Simply never mention anywhere it's an address, and Chrome will stop doing this.

I hope this can help others.

Solution 21 - Html

<input id="autocomplete" autocomplete="nope" type="text" onFocus="this.setAttribute('autocomplete','nope'); geolocate();">

Solution 22 - Html

You can avoid the Chrome autofill overlay by placing the places autocomplete <input> field outside of the <form> element (tested in Chrome 80.0.3987.149).

That does of course introduce new issues in terms of structuring and styling your HTML.

I recommend to have input fields such as <input name="street" type="hidden" /> or <input name="city" type="hidden" /> inside your <form> element that you then fill after the user selected an address from the places autocomplete widget outside of the <form> element.

Solution 23 - Html

Simply use below code, this should help you. It will set autocomplete attribute to "new-loc" (you can add any custom value like new-address etc.).

var input = $("#myaddress");
this.$window.google.maps.event.addDomListener(input[0], 'focusin', e => e.target.setAttribute('autocomplete', 'new-location'));

Solution 24 - Html

	var address1 = document.getElementById("myaddress");
	address1.addEventListener("focus", clearDefaultAutoComplete);
	function clearDefaultAutoComplete() {
		address1.removeAttribute("autocomplete");
	  	address1.setAttribute("autocomplete", "no");
	}

Solution 25 - Html

Follow these best practice to achieve this:

  1. Use autocomplete="off" on main <form> element
  2. For the field itself, put autocomplete to anything other than off for e.g `autocomplete="no-autocomplete"
  3. Create input with dynamic name and then use value of this with real field ( put the real field with type="hidden" attribute.
  4. Very Important : Take input type="search" instead of text. In most of cases, this will step only will resolve your problem.

example:

 <form autocomplete="off" method="post">
    <input type="hidden" name="original-input-name" id="originalInput">
    <input type="search" id="fakeInput" name="{any_random_name}" onblur="document.getElementById('originalInput').value = this.value">                                             
</form>

Solution 26 - Html

Setting the input type as type="search" worked for me

Solution 27 - Html

I spent hours searching for the perfect solution that would work in all browsers and finally found it!

TL;DR

Rename your input field names and field ids to something non-related like 'data_input_field_1'. Then add the &#8204; character into the middle of your labels. This is a non-printing character, so you won't see it, but it tricks the browser into not recognizing the field as one needing auto-completing, thus no built-in auto-complete widget is shown!

The Details

Almost all browsers use a combination of the field's name, id, placeholder, and label to determine if the field belongs to a group of address fields that could benefit from auto-completion. So if you have a field like <input type="text" id="address" name="street_address"> pretty much all browsers will interpret the field as being an address field. As such the browser will display its built-in auto-completion widget. The dream would be that using the attribute autocomplete="off" would work, unfortunately, most browsers nowadays don't obey the request.

So we need to use some trickery to get the browsers to not display the built-in autocomplete widget. The way we will do that is by fooling the browser into believing that the field is not an address field at all.

Start by renaming the id and the name attributes to something that won't give away that you're dealing with address-related data. So rather than using <input type="text" id="city-input" name="city">, use something like this instead <input type="text" id="input-field-3" name="data_input_field_3">. The browser doesn't know what data_input_field_3 represents. But you do.

If possible, don't use placeholder text as most browsers will also take that into account. If you have to use placeholder text, then you'll have to get creative and make sure you're not using any words relating to the address parameter itself (like City). Using something like Enter location can do the trick.

The final parameter is the label attached to the field. However, if you're like me, you probably want to keep the label intact and display recognizable fields to your users like "Address", "City", "State", "Country". Well, great news: YOU CAN! The best way to achieve that is to insert a Zero-Width Non-Joiner Character &#8204; as the second character in the label. So replacing <label>City</label> with <label>C&#8204;ity</label>. This is a non-printing character, so your users will see City, but the browser will be tricked into seeing C ity and not recognize the field!

Mission accomplished! If all went well, the browser should not display the built-in address auto-completion widget on those fields anymore!

Hope this helps you in your endeavors!

Solution 28 - Html

It's just a simple solution, Don't use standard autocomplete phrases in name, id and placeholder. And add autocomplete="off".

All works on version 90.0.4430.212

I use data-name instead of name

<input value="Russia" data-name="country" type="text" placeholder="Write something" autocomplete="off">

Check the list of phrases here: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete

Solution 29 - Html

For those using WooCommerce and "Woocommerce autocomplete checkout address" plugin, I was able to solve the issue by adding this line to the existing billing_address_1 field:

var billing_address = document.getElementById("billing_address_1");
  if(billing_address != null){
            billing_address.addEventListener("focus", function( event ) {
                if(navigator.userAgentData?.brands?.some(b => b.brand === 'Google Chrome'))  //<-- Change made starts here!
                {
                    billing_address.setAttribute('autocomplete', 'none')   
                }                                                                           //<-- Change made ends here!
                RpCheckoutAutocomplete.method.setAutocompleteCountry()
            }, true);
        } 

and this line to the shipping_address_1 field:

var shipping_address = document.getElementById("shipping_address_1");
        if(shipping_address != null){
            shipping_address.addEventListener("focus", function( event ) {
               if(navigator.userAgentData?.brands?.some(b => b.brand === 'Google Chrome'))  //<-- Change made starts here!
                {
                    shipping_address.setAttribute('autocomplete', 'none')   
                }                                                                           //<-- Change made ends here!
                RpCheckoutAutocomplete_shipping.method.setAutocompleteCountry()
            }, true);
        } 

Location of the change -> wp-content/plugins/woo-autocomplete-checkout-address/autocomplete.js

Solution 30 - Html

For me I just had to put something random in the autocomplete attribute like this

<input type="text" autocomplete="chromeisstupid"/>

Solution 31 - Html

Bonus: I've made a reduced test-case Codepen to demonstrate how to accomplish this based on Rimmel's comment that also includes a way to disable autofill for 1password 

https://codepen.io/ls-andrew-borstein/pen/VwWjoEM

<label for="search-address">Address</label>
<input id="search-address" autocomplete="new-password" placeholder="Street and number">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCole WaldripView Question on Stackoverflow
Solution 1 - HtmlRimmelView Answer on Stackoverflow
Solution 2 - HtmlPlayer 0001View Answer on Stackoverflow
Solution 3 - HtmlNoor MuhammadView Answer on Stackoverflow
Solution 4 - HtmlMAX POWERView Answer on Stackoverflow
Solution 5 - HtmlNick BView Answer on Stackoverflow
Solution 6 - HtmlSaurish KarView Answer on Stackoverflow
Solution 7 - HtmlPhilip CallenderView Answer on Stackoverflow
Solution 8 - HtmlRonn WilderView Answer on Stackoverflow
Solution 9 - HtmlSascha KirsteinView Answer on Stackoverflow
Solution 10 - HtmlmamislimenView Answer on Stackoverflow
Solution 11 - HtmlAdeesh JainView Answer on Stackoverflow
Solution 12 - HtmlDavid CrawfordView Answer on Stackoverflow
Solution 13 - HtmlbendeckoView Answer on Stackoverflow
Solution 14 - HtmlAnkush GoyalView Answer on Stackoverflow
Solution 15 - HtmlLaiacyView Answer on Stackoverflow
Solution 16 - HtmlYoan PumarView Answer on Stackoverflow
Solution 17 - HtmlHooman BahreiniView Answer on Stackoverflow
Solution 18 - Htmlcode passionateView Answer on Stackoverflow
Solution 19 - HtmlDaniel NituView Answer on Stackoverflow
Solution 20 - HtmlRobousteView Answer on Stackoverflow
Solution 21 - HtmlBrennan JamesView Answer on Stackoverflow
Solution 22 - HtmlTaigView Answer on Stackoverflow
Solution 23 - HtmlRahul JhaView Answer on Stackoverflow
Solution 24 - Htmlubaid ullahView Answer on Stackoverflow
Solution 25 - HtmlSunil KumarView Answer on Stackoverflow
Solution 26 - HtmlDerek BakerView Answer on Stackoverflow
Solution 27 - HtmlRocket FuelView Answer on Stackoverflow
Solution 28 - HtmlPITView Answer on Stackoverflow
Solution 29 - HtmlJoseph L.View Answer on Stackoverflow
Solution 30 - HtmlWarfaceView Answer on Stackoverflow
Solution 31 - HtmlAndrew BorsteinView Answer on Stackoverflow