How to set autocomplete="off" in lightning form
Possible as of Spring 19. See here
autocomplete—Controls autofilling of the field. This attribute is supported for email, search, tel, text, and url input types. The values on and off are supported, but the autofill behavior depends on the browser. When you set the autocomplete attribute, the lightning:input component passes the value through to be interpreted by the browser.
I just spoke with support at Salesforce and apparently this is currently impossible.
(I am leaving my answer here not as the accepted answer because I want someone to find this question when there is a better option and answer it and I will mark them as accepted.)
Workaround:
However, I am using the following hack workaround:
I gave my element an id, and execute this JS:
turnOffAutocomplete : function(component) {
var input = document.getElementById("id-of-the-input");
if(input.getAttribute("autocomplete") !== "off"){
input.setAttribute("autocomplete","off");
}
}
I'm executing it not in init
(because that executes before the elements have rendered) but instead the when the input element is focused.
Any comments to improve this (so it doesn't have to execute every time the input is focused, for instance) would be appreciated.