: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
Blazing fast and lightweight autocomplete widget without dependencies. Only 1KB gzipped.
Demo: https://kraaden.github.io/autocomplete/
If you want to use the library in browser, just include the `autocomplete.js` and `autocomplete.css` into your HTML file.
npm install autocompleter
Then import it into your javascript code:
import autocomplete from 'autocompleter';
var autocomplete = require('autocompleter');
{ label: 'United Kingdom', value: 'UK' },
{ label: 'United States', value: 'US' }
var input = document.getElementById("country");
fetch: function(text, update) {
text = text.toLowerCase();
// you can also use AJAX requests instead of preloaded data
var suggestions = countries.filter(n => n.label.toLowerCase().startsWith(text))
onSelect: function(item) {
input.value = item.label;
[Try online](https://jsbin.com/gocayupedo/edit?html,js,output)
## Use with Typescript and Webpack
Simply import the autocompleter in your typescript file:
import autocomplete from "autocompleter";
and call the `autocomplete` function as showed below:
// replace the `Client` interface with the interface you want to use with autocomplete
input: document.getElementById("myinputfield"),
emptyMsg: "No items found",
fetch: (text: string, update: (items: Client[]) => void) => {
onSelect: (item: Client) => {
If your custom interface doesn't have the `label` property, you might get a compilation error from typescript. In this case just add an additional type to your code and pass it to the autocompleter:
import autocomplete, { AutocompleteItem } from "autocompleter";
// this type will prevent typescript warnings
type AutocompleteClient = Client & AutocompleteItem;
autocomplete<AutocompleteClient>({
input: document.getElementById("myinputfield"),
emptyMsg: "No items found",
fetch: (text: string, update: (items: Client[]) => void) => {
onSelect: (item: Client) => {
render: function(item: Client, currentValue: string): HTMLDivElement | undefined {
const itemElement = document.createElement("div");
itemElement.textContent = item.FirstName;
If your interface doesn't have a `label` property, you also have to provide a custom render function.
You can pass the following options to `autocomplete`:
| Parameter | Description | Default |
| --------- | ----------- | ------- |
|`onSelect`|This method will be called when user choose an item in autocomplete. The selected item will be passed as first parameter.|`-`|
|`input`|DOM input element must be passed with this parameter and autocomplete will attach itself to this field. Selectors are not supported, but you can just use `document.querySelector('...')` to find the required element.|`-`|
|`minLength`|Specify the minimum length, when autocomplete should appear on the screen.|`2`|
|`emptyMsg`|The message that will be showed when there are no suggestions that match the entered value.|`undefined`|
|`render`|This method allows you to override the rendering function. It will be called for each suggestion and the suggestion object will be passed as first parameter. The current input field value will be passed as second parameter. This function must return a DIV element or `undefined` to skip rendering.|`undefined`|
|`renderGroup`|The same as `render`, but will be called for each group. The first parameter of the function will be the group name. The current input field value will be passed as second parameter. This function must return a `DIV` element or `undefined` to skip rendering.|`undefined`|
|`className`|The autocomplete container will have this class name if specified.|`undefined`|
|`fetch`|This method will be called to prepare suggestions and then pass them to autocomplete. The first parameter is the text in the input field. The second parameter is a callback function that must be called after suggestions are prepared with an array as parameter. If you pass `false` to the callback function, autocomplete will show previous suggestions and will not re-render.|`-`|
|`debounceWaitMs`|Enforces that the `fetch` function will only be called once within the specified time frame (in milliseconds) and delays execution. This prevents flooding your server with AJAX requests.|`0`|
|`customize`|Callback for additional autocomplete customization after rendering is finished. Use this function if you want to change autocomplete default position.|`undefined`|
|`preventSubmit`|Prevents automatic form submit when ENTER is pressed.|`false`|
### Sample config using all options
onSelect: function(item) {
input: document.getElementById('myinput'),
emptyMsg: 'No elements found',
render: function(item, currentValue) {
var div = doc.createElement("div");
div.textContent = item.label;
renderGroup: function(groupName, currentValue) {
var div = doc.createElement("div");
div.textContent = groupName;
className: 'autocomplete-customizations',
fetch: function(text, callback) {
text = text.toLowerCase();
var suggestions = [{ label: "United States", value: "US" }];
customize: function(input, inputRect, container, maxHeight) {
### Display autocomplete above the input field
You can use the following snippet to display autocomplete above the input field if there is not enough space for it.
customize: function(input, inputRect, container, maxHeight) {
container.style.top = "";
container.style.bottom = (window.innerHeight - inputRect.bottom + input.offsetHeight) + "px";
container.style.maxHeight = "200px";
If you don't want to pass this function every time, you can also use spread operator to create your own autocomplete version with default implementation:
export default function autocompleteCustomized<T extends AutocompleteItem>(settings: AutocompleteSettings<T>): AutocompleteResult {
customize: (input: HTMLInputElement, inputRect: ClientRect | DOMRect, container: HTMLDivElement, maxHeight: number): void => {
container.style.top = "";
container.style.bottom = (window.innerHeight - inputRect.bottom + input.offsetHeight) + "px";
container.style.maxHeight = "200px";
You can call `destroy` method on the returned object in order to remove event handlers and DOM elements after usage:
var autocompl = autocomplete({ /* options */ });
You can display suggestions separated into one or multiple groups/categories:
{ label: 'Canada', value: 'CA', group: 'North America' },
{ label: 'United States', value: 'US', group: 'North America' },
{ label: 'Uzbekistan', value: 'UZ', group: 'Asia' },
input: document.getElementById("country"),
fetch: function(text, update) {
text = text.toLowerCase();
var suggestions = countries.filter(n => n.label.toLowerCase().startsWith(text))
onSelect: function(item) {
[Try online](http://jsbin.com/sodicopeya/1/edit?html,js,output)
Note: Please make sure that all items are sorted by the group property.
Autocomplete is released under the MIT License.
Copyright (c) 2016 - Denys Krasnoshchok
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN