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: 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: 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.
value=""
, but it has no effect in this situation.
input:-webkit-autofill { display: none; }
, but no cigar.
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']
});
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 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.
autocomplete="off"
on the form and autocomplete="false"
on the hidden input worked like a charm for me. Thank you!
autoComplete="false"
works for a regular input, but breaks after calling new this.props.google.maps.places.Autocomplete
.
Thanks to GSTAR. Instead of Jquery, I used the below code & this worked for me
jQuery solution:
$('#address').focus(function() {
$(this).attr('autocomplete', 'new-password');
});
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'));
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
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.
name
attribute and the workaround was using a random string for name.
id
, name
, and placeholder
attrs that contained "address" in them
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.
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')
})
}
Work on Chrome v 90: Put the following in your
onfocus="$(this).attr('autocomplete', 'no')"
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 !
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.
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');
});
}
});
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>
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
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']
});
}
};
};
})();
I tried the sample from Google's Address Finder, and got the same problem:
https://i.stack.imgur.com/ccZCv.jpg
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
}
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');
}
})
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.
name
attribute and the thread has people reporting the id
attribute can trigger it as well. Setting the field's name/id to some random string instead of "address" does the trick for me.
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.
<input id="autocomplete" autocomplete="nope" type="text" onFocus="this.setAttribute('autocomplete','nope'); geolocate();">
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.
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'));
var address1 = document.getElementById("myaddress");
address1.addEventListener("focus", clearDefaultAutoComplete);
function clearDefaultAutoComplete() {
address1.removeAttribute("autocomplete");
address1.setAttribute("autocomplete", "no");
}
Follow these best practice to achieve this:
Use autocomplete="off" on main
Setting the input type as type="search"
worked for me
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 ‌
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 ‌
as the second character in the label. So replacing <label>City</label>
with <label>C‌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!
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
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
For me I just had to put something random in the autocomplete attribute like this
<input type="text" autocomplete="chromeisstupid"/>
Success story sharing
observerHack.disconnect()
resulted in an undefined error for me, so I removed it and added anif (autocompleteInput.getAttribute('autocomplete') !== 'new-password')
check before setting the attribute.$("#id-of-element").attr("autocomplete", "new-password");
withautocompleteInput.setAttribute("autocomplete", "new-password");