Filtering rendered content with vanilla JavaScript

Long pages can become hard to navigate, and while the browser's built-in find function helps, a page can offer its own search that narrows the visible content down to matches. This only works on text already rendered in the DOM — it is not a database or JSON query.

The technique needs JavaScript to collect the searchable elements, watch the input, and filter each element's innerText with .includes(), then show or hide the parent card depending on whether it matched.

Markup and styles for a searchable card list

A FAQ page makes a natural example: each question is a card with a title and content, and the page holds many of them.

<h1>FAQ Section</h1>

<div class="cards">
  <h3>Who are we</h3>
  <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized </p>
</div>

<div class="cards">
  <h3>What we do</h3>
  <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized </p>
</div>

<div class="cards">
  <h3>Why work here</h3>
  <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized</p>
</div>

<div class="cards">
  <h3>Learn more</h3>
  <p>Want to learn more about us?</p>
</div>

A single line of CSS supplies the class that will be toggled as the search runs:

.is-hidden { display: none; }

The input wires up an event when it is interacted with:

<label for="searchbox">Search</label>
<input 
  type="search" 
  oninput="liveSearch()" 
  id="searchbox" 
>

The search function

function liveSearch() {
  // Locate the card elements
  let cards = document.querySelectorAll('.cards')
  // Locate the search input
  let search_query = document.getElementById("searchbox").value;
  // Loop through the cards
  for (var i = 0; i < cards.length; i++) {
    // If the text is within the card...
    if(cards[i].innerText.toLowerCase()
      // ...and the text matches the search query...
      .includes(search_query.toLowerCase())) {
        // ...remove the `.is-hidden` class.
        cards[i].classList.remove("is-hidden");
    } else {
      // Otherwise, add the class.
      cards[i].classList.add("is-hidden");
    }
  }
}

The code stores references to the cards and the input, then on each search event loops through the cards and checks whether each card's text contains the query. A matching card has .is-hidden removed; a non-matching one keeps it and stays hidden. A demo shows it running.

Debouncing the handler

To keep the function from running too often and slowing the page, liveSearch is called only after a delay.

<!-- Delete on Input event on this input -->
<label for="searchbox">Search</label>
<input type="search" id="searchbox">
// A little delay
let typingTimer;        
let typeInterval = 500; // Half a second
let searchInput = document.getElementById('searchbox');

searchInput.addEventListener('keyup', () => {
  clearTimeout(typingTimer);
  typingTimer = setTimeout(liveSearch, typeInterval);
});

Matching text the user cannot see

Approximating a fuzzy search means letting related keywords return a card even without an exact visible match. One approach is a hidden element such as a span holding keywords:

<div class="cards">
  <h3>Who are we</h3>
  <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized</p>
  
    <!-- Put any keywords here -->
   <span class="is-hidden">secret</span> 
</div>

The function then switches from .innerText to .textContent so hidden elements are included — the MDN docs cover the difference between the two. Typing "secret" reveals the card even though that word never appears on screen.

Alternatively, keywords can live in an attribute. On an image gallery, put them in alt: typing "kitten" or "human" matches the alt text. Here innerText becomes getAttribute('alt') so the attribute is searched alongside visible content.

for (var i = 0; i < cards.length; i++) {
  if(cards[i].getAttribute('alt').toLowerCase()
    .includes(search_query.toLowerCase())) {
      cards[i].classList.remove("is-hidden");
  } else {
    cards[i].classList.add("is-hidden");
  }
}

Other attributes, including custom ones, work just as well depending on your needs.

Limitation

It works only if you have all the searchable content in the DOM on that page, already rendered.

Where it fits

FAQ pages are the obvious candidate, but any content that needs filtering suits the approach — including an image gallery searched through its alt text. A production site, freestuff.dev, uses the same technique, and the point is that a reasonably robust search comes from a few lines of vanilla JavaScript.