Autocomplete Search in JavaScript
Suggest subjects in JavaScript with lowercase matching, filter, and slice as input changes. Show up to five buttons; this is not a full combobox widget.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<label>Find a subject <input id="search" autocomplete="off" type="search"></label><ul id="results"></ul><p id="status">Type a subject name.</p>7<script>8const subjects = ['Biology', 'Business Studies', 'Chemistry', 'Computer Science'];9const input = document.querySelector('#search');10input.oninput = () => {11 const query = input.value.trim().toLowerCase();12 const matches = query ? subjects.filter(subject => subject.toLowerCase().includes(query)).slice(0, 5) : [];13 const list = document.querySelector('#results'); list.replaceChildren();14 for (const subject of matches) {15 const row = document.createElement('li');16 const button = document.createElement('button'); button.textContent = subject;17 button.onclick = () => { input.value = subject; list.replaceChildren(); document.querySelector('#status').textContent = 'Selected: ' + subject; input.focus(); };18 row.append(button); list.append(row);19 }20 document.querySelector('#status').textContent = query ? matches.length + ' suggestions' : 'Type a subject name.';21};22</script>23</body>24</html>
Sample Output
Computer Science
After typing co, only this fixed subject matches the normalized substring.
Selected: Computer Science
Choosing its button copies the subject into the input and clears the suggestion list.
In this one
- Filter text without case sensitivity
- Handle the input event
- Limit visible suggestions
- Choose a suggestion with a real button
How it works
- 1
Keep the four subjects in a local array.
- 2
Normalize the typed query.
- 3
Filter matches and take at most five.
- 4
Replace the old results with buttons for the new matches.
- 5
Type co and choose Computer Science to see the selection message.
Edge cases
Blank query
includes an empty string matches every subject.
The query check deliberately returns an empty result list instead.
No match
A query like astronomy is absent from the list.
The status reports zero suggestions. In a real app you can also offer a way to browse all subjects.
What's going on
Autocomplete search suggests matching items while a person types. This subject finder searches a small local list, so it is a useful first project before dealing with server requests or large data sets.
The input event runs for typing, pasting, and other input changes. trim removes outside spaces, and toLowerCase makes matching ignore capital letters. filter keeps subjects containing the query, while slice limits the display to five results. Empty input shows no suggestions instead of listing every subject.
Each result is an ordinary button that copies the subject into the input. textContent keeps the subject name as text. This is a basic suggestion list, not a complete keyboard-arrow combobox. A combobox is an input connected to a selectable popup list. Here the result buttons can be reached with Tab and activated normally. A full production autocomplete needs additional focus, arrow-key, and screen-reader behavior.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What makes the search case-insensitive?
Pick an answer to check it.
Question 2
Why check query before filter?
Pick an answer to check it.
Questions people ask
Does this send my typing to a server?
No. It searches the fixed array in the page.
Why use input rather than keyup?
input also covers changes such as pasting text. It tracks value changes more directly.
Can I navigate suggestions with arrow keys?
Not in this basic version. Use Tab to reach the buttons; a complete combobox needs more keyboard handling.