How to create a filter list with JavaScript?

JavaScript filter lists allow users to search and filter through data dynamically. This is commonly used for search functionality in websites and applications.

Complete HTML Example

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <style>
      * {
         box-sizing: border-box;
      }
      .searchInput {
         width: 100%;
         font-size: 16px;
         padding: 12px 20px 12px 40px;
         border: 2px solid grey;
         margin-bottom: 12px;
      }
      .animalUL {
         list-style-type: none;
         padding: 0;
         margin: 0;
      }
      .animalUL li a {
         border-top: 1px solid rgb(0, 0, 0);
         margin-top: -1px;
         background-color: #f6f6f6;
         padding: 12px;
         text-decoration: none;
         font-size: 18px;
         color: black;
         display: block;
      }
      .animalUL li a:hover:not(.header) {
         background-color: #eee;
      }
   </style>
</head>
<body>
   <h1>Filter List Example</h1>
   <input type="text" class="searchInput" onkeyup="filterFunction()" placeholder="Search for animals" title="Type in a name"/>
   <ul class="animalUL">
      <li><a href="#">Cat</a></li>
      <li><a href="#">Cheetah</a></li>
      <li><a href="#">Dog</a></li>
      <li><a href="#">Giraffe</a></li>
      <li><a href="#">Lion</a></li>
      <li><a href="#">Leopard</a></li>
      <li><a href="#">Llama</a></li>
   </ul>
   
   <script>
      function filterFunction() {
         var input, filter, ul, li, a, i, value;
         input = document.querySelector(".searchInput");
         filter = input.value.toUpperCase();
         ul = document.querySelector(".animalUL");
         li = ul.getElementsByTagName("li");
         
         for (i = 0; i < li.length; i++) {
            a = li[i].getElementsByTagName("a")[0];
            value = a.textContent || a.innerText;
            if (value.toUpperCase().indexOf(filter) > -1) {
               li[i].style.display = "";
            } else {
               li[i].style.display = "none";
            }
         }
      }
   </script>
</body>
</html>

How It Works

The filter function performs these steps:

  1. Get input value: Retrieves the search term and converts to uppercase for case-insensitive matching
  2. Loop through list items: Iterates through each <li> element in the list
  3. Extract text content: Gets the text from each list item using textContent or innerText
  4. Match and display: Shows items containing the search term, hides others

Modern ES6 Version

<script>
   function filterList() {
      const input = document.querySelector('.searchInput');
      const filter = input.value.toLowerCase();
      const items = document.querySelectorAll('.animalUL li');
      
      items.forEach(item => {
         const text = item.textContent.toLowerCase();
         item.style.display = text.includes(filter) ? '' : 'none';
      });
   }
   
   document.querySelector('.searchInput').addEventListener('input', filterList);
</script>

Key Features

  • Real-time filtering: Results update as user types
  • Case-insensitive: Matches regardless of letter case
  • Partial matching: Finds items containing the search term
  • DOM manipulation: Uses display property to show/hide items

Output

The filter list displays all animals initially. When you type in the search box, only matching animals remain visible:

Filter List Example Search for animals Cat Cheetah Dog ... (more items) After typing "ca": Cat (Other items hidden)

Conclusion

JavaScript filter lists provide an intuitive way to search through data. The key is using string methods like indexOf() or includes() with DOM manipulation to show/hide matching elements dynamically.

Updated on: 2026-03-15T23:18:59+05:30

595 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements