Autocomplete
Use the autocomplete
attribute for fields that collect user data.
The autocomplete
attribute is an HTML attribute that can be used to control the behavior of a
form input element when a user types into it.
It allows developer to specify whether the browser's autocomplete feature should be enabled for that input
field. In HTML 5.2, the autocomplete
attribute also supports a variety of tokens that allow
developers to provide more specific instructions to the browser about how to autocomplete the input field.
NOTE:
- New to accessibility or uncertain of requirements, it will be helpful to review all sections below.
- Already familiar with requirements, skip to the “Working Example” section for sample HTML, CSS and JavaScript (when needed), along with a working demo.
- Provide autocomplete attribute to input fields along with appropriate values.
- The autocomplete attribute can be used on various types of form input fields such as "First Name", "Last Name", "Email", "Phone" and so on.
-
Use appropriate values for the autocomplete attribute, such as
on
,off
,given-name
,family-name
,email
,tel
and so on. -
When the
autocomplete
attribute is seton
, the browser will attempt to automatically fill in the input field based on the user’s previous input or other data that the browser has collected. -
When the
autocomplete
attribute is setoff
, the browser will not offer any suggestions or autofill the input field. -
The HTML 5.2
autocomplete
attribute supports a variety of values that allows to provide more specific instructions to the browser about how to autocomplete the input fields. They are as follows as mentioned in HTML 5.2 autofill tokens.- name – Full name
- honorific-prefix – Prefix or title (e.g., "Mr.", "Ms.", "Dr.", "Mlle")
- given-name – Given name (in some Western cultures, also known as the first name)
- additional-name – Additional names (in some Western cultures, also known as middle names, forenames other than the first name)
- family-name – Family name (in some Western cultures, also known as the last name or surname)
- honorific-suffix – Suffix (e.g., "Jr.", "B.Sc.", "MBASW", "II")
- nickname – Nickname, screen name, handle: a typically short name used instead of the full name
- organization-title – Job title (e.g., "Software Engineer", "Senior Vice President", "Deputy Managing Director")
- username – A username
- new-password – A new password (e.g., when creating an account or changing a password)
- current-password – The current password for the account identified by the username field (e.g., when logging in)
- organization – Company name corresponding to the person, address, or contact information in the other fields associated with this field
- street-address – Street address (multiple lines, newlines preserved)
- address-line1 – Street address (one line per field, line 1)
- address-line2 – Street address (one line per field, line 2)
- address-line3 – Street address (one line per field, line 3)
- address-level4 – The most fine-grained administrative level, in addresses with four administrative levels
- address-level3 – The third administrative level, in addresses with three or more administrative levels
- address-level2 – The second administrative level, in addresses with two or more administrative levels; in the countries with two administrative levels, this would typically be the city, town, village, or other locality within which the relevant street address is found
- address-level1 – The broadest administrative level in the address, i.e., the province within which the locality is found; for example, in the US, this would be the state; in Switzerland it would be the canton; in the UK, the post-town
- country &ndash Country code
- country-name – Country name
- postal-code – Postal code, post code, ZIP code, CEDEX code (if CEDEX, append "CEDEX", and the dissement, if relevant, to the address-level2 field)
- cc-name – Full name as given on the payment instrument
- cc-given-name – Given name as given on the payment instrument (in some Western cultures, also known as the first name)
- cc-additional-name – Additional names given on the payment instrument (in some Western cultures, also known as middle names, forenames other than the first name)
- cc-family-name – Family name given on the payment instrument (in some Western cultures, also known as the last name or surname)
- cc-number – Code identifying the payment instrument (e.g., the credit card number)
- cc-exp – Expiration date of the payment instrument
- cc-exp-month – Month component of the expiration date of the payment instrument
- cc-exp-year – Year component of the expiration date of the payment instrument
- cc-csc – Security code for the payment instrument (also known as the card security code (CSC), card validation code (CVC), card verification value (CVV), signature panel code (SPC), credit card ID (CCID), etc.)
- cc-type – Type of payment instrument
- transaction-currency – The currency that the user would prefer the transaction to use
- transaction-amount – The amount that the user would like for the transaction (e.g., when entering a bid or sale price)
- language – Preferred language
- bday – Birthday
- bday-day – Day component of birthday
- bday-month – Month component of birthday
- bday-year – Year component of birthday
- sex – Gender identity (e.g., Female, Fa&8217;afafine)
- url – Home page or other Web page corresponding to the company, person, address, or contact information in the other fields associated with this field
- photo – Photograph, icon, or other image corresponding to the company, person, address, or contact information in the other fields associated with this field
- tel – Full telephone number, including country code
- tel-country-code – Country code component of the telephone number
- tel-national – Telephone number without the county code component, with a country-internal prefix applied if applicable
- tel-area-code – Area code component of the telephone number, with a country-internal prefix applied if applicable
- tel-local – Telephone number without the country code and area code components
- tel-local-prefix – First part of the component of the telephone number that follows the area code, when that component is split into two components
- tel-local-suffix – Second part of the component of the telephone number that follows the area code, when that component is split into two components
- tel-extension – Telephone number internal extension code
- email – E-mail address
- impp – URL representing an instant messaging protocol endpoint (for example, "aim:goim?screenname=example" or "xmpp:fred@example.net")
For example,
<div class="autocomplete">
<label for="fullName">Full Name:</label>
<!-- Autocomplete for Full name field -->
<input name="fullName" id="fullName" class="autocomplete_input" type="text" autocomplete="name">
<label for="email">Email:</label>
<!-- Autocomplete for Email field -->
<input name="email" class="autocomplete_input" id="email" type="email" autocomplete="email">
</div>
aria-autocomplete attribute
The aria-autocomplete
can be used to indicate whether inputting text could trigger display of
one or more suggestions or predictions.
Can be used for a combobox, searchbox, or textbox and specifies how suggestions or predictions will be presented.
-
Provide
aria-autocomplete
attribute to textbox, searchbox, or combobox along with appropriate values. -
Use appropriate values for the
aria-autocomplete
attribute, such as none, inline, list and both as per requirement. - Set
aria-autocomplete
attribute to none if no suggestions are displayed. -
When the
aria-autocomplete
attribute is set to inline, the suggested options will be displayed directly within the combobox input field. -
When the
aria-autocomplete
attribute is set to list, the suggested options will be displayed in a separate list that appears when the user interacts with the combobox. -
When the
aria-autocomplete
attribute is set to both, the suggested options can be displayed in the both models at the same time such as inline and in a separate list.
for example,
<div class="input-container open" id="combo-input">
<h2>Combobox Autocomplete</h2>
<!-- Combobox -->
<label for="my-combobox">Find your eTextbook:</label>
<input type="text" id="my-combobox" aria-autocomplete="list" role="combobox" aria-expanded="true" aria-controls="my-listbox">
<button type="button" id="arw-downbtn" class="arrow-dwn" aria-label="eTextbook" tabindex="-1" aria-controls="my-listbox" aria-expanded="true">
<i class="fa-solid fa-chevron-down"></i>
</button>
</div>
<!-- Combobox options -->
<ul id="my-listbox" class="list-items" role="listbox" aria-label="eTextbook">
<li role="option" id="option-0">Foundations of Decision Analysis</li>
<li role="option" id="option-1">Optimization in Operations Research</li>
...
</ul>
Implementing autocomplete and aria-autocomplete attribute correctly benefits majorly the below users.
- People with cognitive disabilities
- People with visual disabilities
- People using speech input
- People with limited dexterity
- People using keyboard only
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
<h2>Autocomplete Attribute</h2>
<div class="autocomplete">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" class="autocomplete_input" autocomplete="given-name">
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" class="autocomplete_input" autocomplete="family-name">
<label for="fullName">Full Name</label>
<input type="text" name="fullName" id="fullName" class="autocomplete_input" autocomplete="name">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="autocomplete_input" autocomplete="email">
</div>
<h2>Comobobox with aria-autocomplete="list"</h2>
<div class="combobox-container">
<div class="input-container" id="combo-input">
<label for="my-combobox">Find your eTextbook</label>
<input type="text" id="my-combobox" aria-autocomplete="list" role="combobox" aria-expanded="false" aria-controls="my-listbox" />
<button type="button" id="arw-downbtn" class="arrow-dwn" aria-label="eTextbook" tabindex="-1" aria-controls="my-listbox" aria-expanded="false"><i class="fa-solid fa-chevron-down"></i>
</button>
</div>
<ul id="my-listbox" class="list-items" role="listbox" aria-label="eTextbook"></ul>
</div>
#my-listbox {
border: 1px solid #ccc;
list-style: none;
position: relative;
margin-top: 2px;
border-radius: 8px;
padding: 16px;
background-color: #fff;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
display: none;
cursor: pointer;
}
.active {
background-color: #e8eefa;
border: 1px solid #000;
}
#my-listbox li {
display: flex;
align-items: center;
height: 40px;
transition: 0.3s;
padding: 0 15px;
border-radius: 8px;
}
.autocomplete {
position: relative;
max-width: 334px;
width: 100%;
margin: 3rem 2rem;
}
#my-combobox {
margin-top: 8px;
font-size: 14px;
width: 85%;
padding: 0.8rem;
border: 1px solid #8e8e8e;
line-height: 1.15;
border-radius: 4px;
}
button {
padding: 6px 10px;
margin-top: 13px;
margin-right: 8px;
background: #ddd0;
font-size: 17px;
border: none;
cursor: pointer;
position: absolute;
right: 1.1rem;
}
@media screen and (max-width: 600px) {
#my-combobox {
float: none;
}
.topnav a, .topnav input[type=text], button {
float: none;
text-align: left;
margin: 0;
padding: 14px;
}
input[type=text] {
border: 1px solid #ccc;
}
}
.combobox-container {
position: relative;
max-width: 334px;
width: 100%;
margin: 3rem 2rem;
}
.input-container.open ~ .list-items {
display: block !important;
}
.input-container.open .arrow-dwn {
transform: rotate(-180deg);
}
label {
display: block;
margin-top: 1rem;
font-size: 1.1rem;
}
.autocomplete_input {
display: block;
margin-bottom: 0.2rem;
padding: 0.8rem;
border: 1px solid #8e8e8e;
line-height: 1.15;
width: 90%;
border-radius: 4px;
}
const combobox = document.getElementById('my-combobox');
const comobbtn = document.getElementById('arw-downbtn');
const listbox = document.getElementById('my-listbox');
const inputcontainer = document.getElementById('combo-input');
// Replace with your options
let options = [
'Foundations of Decision Analysis',
'Optimization in Operations Research',
'First Course in Probability',
'Development Across the Life Span',
'Introduction to Financial Accounting',
'Starting Out with Python',
'Psychological Disorders'];
let activeItemId = null;
let listboxVisible = false;
const openListbox = () => {
const value = combobox.value.toLowerCase();
listbox.innerHTML = '';
options.filter(option => option.toLowerCase().includes(value)).forEach((option, index) => {
const li = document.createElement('li');
li.textContent = option;
li.setAttribute('role', 'option');
li.id = `option${index}`;
listbox.appendChild(li);
});
inputcontainer.classList.add('open');
combobox.setAttribute('aria-expanded', 'true');
comobbtn.setAttribute('aria-expanded', 'true');
combobox.focus();
listboxVisible = true;
};
const closeListbox = () => {
listbox.innerHTML = '';
inputcontainer.classList.remove('open');
combobox.setAttribute('aria-expanded', 'false');
comobbtn.setAttribute('aria-expanded', 'false');
combobox.focus();
listboxVisible = false;
};
combobox.addEventListener('input', () => {
const value = combobox.value.toLowerCase();
listbox.innerHTML = '';
openListbox();
});
combobox.addEventListener('keydown', e => {
const activeOption = listbox.querySelector('[aria-selected="true"]');
switch (e.key) {
case 'ArrowUp':
if (activeOption) {
const prevOption = activeOption.previousElementSibling;
if (prevOption) {
activeOption.setAttribute('aria-selected', 'false');
prevOption.setAttribute('aria-selected', 'true');
prevOption.classList.add('active');
activeOption.classList.remove('active');
activeItem = prevOption.id;
}
} else {
const lastOption = listbox.lastElementChild;
if (lastOption) {
lastOption.setAttribute('aria-selected', 'true');
lastOption.classList.add('active');
activeItem = lastOption.id;
}
}
break;
case 'ArrowDown':
if (activeOption) {
const nextOption = activeOption.nextElementSibling;
if (nextOption) {
activeOption.setAttribute('aria-selected', 'false');
nextOption.setAttribute('aria-selected', 'true');
nextOption.classList.add('active');
activeOption.classList.remove('active');
activeItem = nextOption.id;
}
} else {
openListbox();
const firstOption = listbox.firstElementChild;
if (firstOption) {
firstOption.setAttribute('aria-selected', 'true');
firstOption.classList.add('active');
activeItem = firstOption.id;
}
}
break;
case 'Enter':
if (activeOption) {
combobox.value = activeOption.textContent;
closeListbox();
}
break;
case 'Escape':
closeListbox();
break;
default:
break;
}
if (activeItem) {
combobox.setAttribute('aria-activedescendant', activeItem);
}
});
listbox.addEventListener('click', e => {
if (e.target.getAttribute('role') === 'option') {
combobox.value = e.target.textContent;
activeItemId = e.target.id;
closeListbox();
}
});
combobox.addEventListener('click', () => {
if (listboxVisible) {
closeListbox();
} else {
openListbox();
}
});
comobbtn.addEventListener('click', () => {
if (listboxVisible) {
closeListbox();
} else {
openListbox();
}
});