Filterable List Template

An interactive list that users can instantly filter by typing into a search field.

This component is perfect for directories, product lists, or any long list that benefits from a simple, real-time search function to help users find what they need quickly.

A screenshot of a list with a search bar above it, filtering the results.

Get Source Code Preview

About this Template

This component uses vanilla JavaScript to add a useful live search feature to a standard list. It listens for the input event on the search field and, on every keystroke, loops through the list items to show or hide them based on the user's query. The search is case-insensitive for a better user experience and includes a "no results" message for when a search returns empty.

Features

Code Breakdown

The HTML consists of a search input and an unordered list (ul) that has an id. The search input has an aria-controls attribute that points to the list's id, programmatically linking them.

The JavaScript first selects the necessary elements: the input field, all list items, and the "no results" message element. An event listener is attached to the input to fire on the input event.

Inside the event handler, the script gets the search term and converts it to lowercase. It then iterates through each list item. For each item, it gets the item's text, converts it to lowercase, and checks if it includes the search term. Based on the result, it toggles the item's hidden attribute. After checking all items, it updates the visibility of the "no results" message based on whether any items are currently visible.

Code

Here's the full code for the template:

View Output