Home / Forms and Input / Autocomplete Search

Autocomplete Search in JavaScript, Free with Live Demo

Free autocomplete search in plain JavaScript. Suggestions as you type with fuzzy matching, highlighted matches, arrow keys, Enter and Escape, recent searches and ARIA combobox.

Open live demoDownload HTML fileView code on GitHub
Autocomplete Search JavaScript project: a search box that suggests results as you type, with keyboard control

Runs on: ARIA combobox and debounced input. Works in all modern browsers.

What is the Autocomplete Search?

A search box that suggests cities as you type. It matches the start of words first, then anywhere in the name, and forgives a missing letter.

Everything works with the keyboard, matching letters are highlighted, and your last five picks show as recent searches when the box is empty.

Good for

  • Site search boxes
  • Picking a city, country or product
  • Tagging and mentions
  • Admin panels with long lists

What this project does

How it works

  1. Score every itemA starts-with match scores highest, then contains, then a loose letter-by-letter match that allows a small gap, so dhka still finds Dhaka.
  2. Combobox rolesThe input has role combobox and points to the listbox. aria-activedescendant tells screen readers which option is highlighted while focus stays in the box.
  3. Wait a momentA short debounce means the list only updates after a pause in typing, which matters when results come from a server.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

input.addEventListener("input", () => {
  clearTimeout(timer);
  timer = setTimeout(() => {
    const q = input.value.trim().toLowerCase();
    const results = data
      .map((item) => [item, score(q, item.name)])
      .filter(([, s]) => s > 0)
      .sort((a, b) => b[1] - a[1])
      .slice(0, 8);
    render(results);
  }, 120);
});
input.setAttribute("aria-activedescendant", "option-" + active);

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

How do I search a server instead?

Replace the local filter with a fetch to your search endpoint inside the debounced function, and cancel older requests with AbortController.

What is aria-activedescendant?

It lets focus stay in the input while telling screen readers which suggestion is currently highlighted.

How many suggestions should I show?

Between five and ten. More than that is hard to scan.

More Forms and Input projects