Skip to content

Autocomplete API ​

js
const autocomplete = document.querySelector('awc-autocomplete');

Methods ​

open ​

Whether the listbox is visible or not.

js
autocomplete.open;
// => false

show ​

Shows the listbox.

js
autocomplete.show();

hide ​

Hides the listbox.

js
autocomplete.hide();

value ​

Returns all the selected values.

js
autocomplete.value;
// => ['1', '2']

setValue ​

Sets the value of the element.

js
autocomplete.setValue('apple', 'Apple');
autocomplete.value;
// => ['apple']

// When the options are not loaded from the server, you do not have to pass the text argument.
autocomplete.setValue('apple');
autocomplete.value;
// => ['apple']

removeValue ​

Removes a value from the element.

js
autocomplete.removeValue('apple');
autocomplete.value;
// => []

TIP

You do not have to provide the argument to removeValue if the element is a single select.

activate ​

Activates an option by setting the data-active attribute.

js
const option = autocomplete.options[0];
autocomplete.activate(option, { scroll: true });

deactivate ​

Deactivates the active option by removing the data-active attribute.

js
autocomplete.deactivate();

clear ​

Deselects all the selected options.

js
autocomplete.clear();

reset ​

Resets the autocomplete value to its initial state.

js
autocomplete.reset();

focus ​

Sets the focus on the input element. This method also accepts a list of focus options.

js
autocomplete.focus();

blur ​

Removes keyboard focus from the input element.

js
autocomplete.blur();

activeOption ​

Returns the option that has the data-active attribute.

js
autocomplete.activeOption;

visibleOptions ​

Returns all the visible options of the autocomplete element.

js
autocomplete.visibleOptions;

options ​

Returns all the options of the autocomplete element.

js
autocomplete.options;

form ​

Returns the parent form element.

js
autocomplete.form;

Events ​

NameBubblesDescription
awc-autocomplete:showtrueThis event fires immediately when the open attribute is added.
awc-autocomplete:showntrueThis event fires when the listbox has been made visible to the user.
awc-autocomplete:hidetrueThis event fires immediately when the open attribute is removed.
awc-autocomplete:hiddentrueThis event fires when the listbox has been completely hidden from the user.
awc-autocomplete:committrueThis event fires when an option is selected or deselected by clicking the option element. You can access the committed option, its value, and its text from the event.detail object.
awc-autocomplete:cleartrueThis event fires when all the selected options have been deselected by clicking on the "Clear" button.
awc-autocomplete:removetrueThis event fires when a tag has been removed. You can access the removed tag, its value, and its text from the event.detail object.
awc-autocomplete:resettrueThis event fires when the parent form has been reset.
loadstartfalseThis event fires when the autocomplete starts the network request.
loadfalseThis event fires when the autocomplete fetches the options successfully from the server.
errorfalseThis event fires when the autocomplete fails to fetch the options from the server.
loadendfalseThis event fires when the autocomplete finishes the network request.

Released under the MIT License.