Powerful Autocomplete With Asynchronous Data Fetch – autocomplete.js
1. Download and import the autocomplete.css & autocomplete.min.js into the document.
<link rel="stylesheet" href="/path/to/autocomplete.css" /> <script src="/path/to/autocomplete.min.js"></script>
2. Load the polyfills for users who are still using the legacy browsers.
if (!('Promise' in window)) {
var script = document.createElement('script');
script.src =
'https://polyfill.io/v3/polyfill.min.js?features=Promise%2CElement.prototype.closest';
document.getElementsByTagName('head')[0].appendChild(script);
} 3. Initialize the autocomplete.js on the target input field and fetch data from an external data source as follows:
<input type="text" id="example" placeholder="Type Something" />
new Autocomplete('example', {
onSearch: ({ currentValue }) => {
const api = `/path/to/datasource/`;
return new Promise((resolve) => {
fetch(api)
.then((response) => response.json())
.then((data) => {
resolve(data);
})
.catch((error) => {
console.error(error);
});
});
},
onResults: ({ matches }) => {
return matches
.map(el => {
return `
<li>${el.name}</li>`;
}).join('');
}
}); 4. The library also supports local data.
new Autocomplete('local', {
onSearch: ({ currentValue }) => {
const data = [
{ "name": "JavaScript" },
{ "name": "CSS" },
{ "name": "HTML" },
{ "name": "Python" }
];
return data.sort((a, b) => a.name.localeCompare(b.name))
.filter(element => {
return element.name.match(new RegExp(currentValue, 'i'))
})
},
onResults: ({ matches }) => {
return matches
.map(el => {
return `
<li>${el.name}</li>`;
}).join('');
}
}); 5. Possible options and functions.
new Autocomplete('example', {
// search delay
delay: 500,
// shows clear button
clearButton: true,
// adds a button to remove text from the input field visible on initial
clearButtonOnInitial: false,
// auto select the first result
selectFirst: false,
// adds text to the input field as you move through the results with the up/down cursors
insertToInput: false,
// the number of characters entered to trigger the search
howManyCharacters: 1,
// name of the class by which you will name the group element
classGroup: 'group-by',
// disable close on select
disableCloseOnSelect: false,
// toggle showing all values when the input is clicked, like a default dropdown
showAllValuesOnClick: false,
// displays all results without clicking on the input field
inline: false,
// prevents results from hiding after clicking on element with this class
classPreventClosing: ``,
// determine whether to cache characters entered in the input field
cache: false,
// prefix all autocomplete css class name
classPrefix: 'auto-',
// prevent the results from scrolling up when we have scrolling
preventScrollUp: false,
// delete the results when the input is empty
removeResultsWhenInputIsEmpty: false,
// modify string before search
regex: { expression: /[|\{}()[]^$+*?]/g, replacement: "\$&" },
onResults = ({ currentValue, matches, template, classGroup }) => { },
onSearch = ({ currentValue }) => { },
onSubmit = ({ index, element, object, results }) => { },
onOpened = ({ type, element, results }) => { },
onReset = (element) => { },
onRender = ({ element, results }) => { },
onClose = () => { },
noResults = ({ element, currentValue, template }) => { },
onSelectedItem = ({ index, element, object, currentValue }) => { },
}); 6. API methods:
instance.destroy(); instance.rerender(); instance.rerender(string); instance.disable(); instance.disable(true); instance.enable();
v3.0.4 (01/15/2026)
v3.0.3 (10/04/2025)
v3.0.2 (09/21/2025)
v3.0.1 (09/19/2025)
v3.0.0 (06/02/2024)
v2.0.2 (06/15/2024)
v2.0.1 (03/26/2024)
v2.0.0 (03/24/2024)
v1.9.0 (10/05/2023)
v1.8.8 (05/23/2023)
v1.8.6 (11/11/2022)
v1.8.5 (07/02/2022)
v1.8.4 (04/12/2022)
v1.8.3 (03/11/2022)
v1.8.2 (03/05/2022)
v1.8.1 (03/03/2022)
v1.8.0 (03/01/2022)
v1.7.5 (02/11/2022)
v1.7.4 (01/21/2022)
v1.7.3 (01/10/2022)
v1.7.2 (12/30/2021)
v1.7.1 (12/14/2021)
v1.7.0 (11/19/2021)
v1.6.9 (11/15/2021)
v1.6.8 (11/13/2021)
v1.6.6 (11/07/2021)
v1.6.5 (11/04/2021)
v1.6.4 (11/04/2021)
v1.6.3 (09/26/2021)
v1.6.1 (09/13/2021)
v1.6.0 (09/04/2021)
v1.5.0 (08/31/2021)
v1.1.4 (08/14/2021)
v1.1.2 (08/14/2021)
v1.1.0 (08/11/2021)
v1.0.44 (06/30/2021)
The post Powerful Autocomplete With Asynchronous Data Fetch – autocomplete.js appeared first on CSS Script.