Skip to content

Instantly share code, notes, and snippets.

@tbveralrud
Last active March 27, 2019 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbveralrud/fcdde1027d8c9f1ec06971b462c2a512 to your computer and use it in GitHub Desktop.
Save tbveralrud/fcdde1027d8c9f1ec06971b462c2a512 to your computer and use it in GitHub Desktop.
How to escape implicit autofill values in HTML label elements
// Browsers don't respect `autocomplete='off'`.
// Safari (maybe others) will inspect the text value of a `label` element to predict intent.
// Sometimes we want to include 'name' in the value and not have it interpreted as a 'user name'.
// This function will transform the string to a visually equivalent string by inserting `zero width join` characters.
const wordJoiner = '\u{2060}';
export function escapeImplicitAutofillValue(label: string): string {
return label.split('').join(wordJoiner);
}
@tbveralrud
Copy link
Author

Note: Screen readers will read each letter individually. Use aria-label with the unescaped string to speak the label correctly.

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