Accessibility in EJ2 JavaScript Auto complete control

13 Dec 20248 minutes to read

The AutoComplete component has been designed, keeping in mind the WAI-ARIA specifications, and applies the WAI-ARIA roles, states, and properties along with keyboard support. This component is characterized by complete keyboard interaction support and ARIA accessibility support that makes it easy for people who use assistive technologies (AT) or those who completely rely on keyboard navigation.

ARIA attributes

The AutoComplete component uses the combobox role and each list item has an option role. The following ARIA Attributes denote the AutoComplete state.

Property Functionalities
aria-haspopup Indicates whether the AutoComplete input element has a suggestion list or not.
aria-expanded Indicates whether the suggestion list has expanded or not.
aria-selected Indicates the selected option from the list.
aria-readonly Indicates the readonly state of the AutoComplete element.
aria-disabled Indicates whether the AutoComplete component is in a disabled state or not.
aria-activedescendent This attribute holds the ID of the active list item to focus its descendant child element.
aria-owns This attribute contains the ID of the suggestion list to indicate popup as a child element.
aria-autocomplete This attribute contains the ‘both’ to a list of options shows and the currently selected suggestion also shows inline.

Keyboard interaction

You can use the following key shortcuts to access the AutoComplete without interruptions.

Keyboard shortcuts Actions
Arrow Down In popup hidden state, opens the suggestion list. In popup open state, selects the first item when no item selected else selects the item next to the currently selected item.
Arrow Up In popup hidden state, opens the suggestion list. In popup open state, selects the last item when no item selected else selects the item previous to the currently selected one.
Page Down Scrolls down to the next page and selects the first item when popup list opens.
Page Up Scrolls up to previous page and select the first item when popup list open.
Enter Selects the focused item and set to AutoComplete component.
Tab Focuses on the next tab indexed element when the popup is closed. Otherwise, closes the popup list and remains the focus in component suppose if it is in an open state.
Shift + tab Focuses the previous tab indexed element when the popup is closed. Otherwise,closes the popup list and remains the focus in component suppose if it is in an open state.
Alt + Down Opens the popup list.
Alt + Up In popup hidden state, opens the popup list. In popup open state, closes the popup list.
Esc(Escape) Closes the popup list when it is in an open state then remove the selection.
Home Cursor moves to before of first character in input.
End Cursor moves to next of last character in input.

In the below sample, focus the AutoComplete component using alt+t keys.

var gameList = [
        { id: 'Game1', game: 'Badminton' },
        { id: 'Game2', game: 'Basketball' },
        { id: 'Game3', game: 'Cricket' },
        { id: 'Game4', game: 'Football' },
        { id: 'Game5', game: 'Golf' },
        { id: 'Game6', game: 'Hockey' },
        { id: 'Game7', game: 'Rugby' },
        { id: 'Game8', game: 'Snooker' },
        { id: 'Game9', game: 'Tennis' },
    ];

// initialize AutoComplete component
var acObject = new ej.dropdowns.AutoComplete({
    //set the data to dataSource property
    dataSource: gameList,
    //map to colum to fields
    fields: { value: 'game' },
    // set placeholder to AutoComplete input element
    placeholder: "Select games",
    // set the popup list height
    popupHeight: '200px'
});

// render initialized AutoComplete
acObject.appendTo('#atcelement');

document.onkeyup = function (e) {
    if (e.altKey && e.keyCode === 84 /* t */) {
        // press alt+t to focus the control.
        acObject.focusIn();
    }
};
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 AutoComplete</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="styles.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container" style="margin:0 auto; width:250px;">
        <input type="text" tabindex="1" id="atcelement">
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>