How to Build a Search Bar
Tags: webdev, javascript, html, css
Published: 2021-09-05 23:08:54

*A quick walkthrough on how to build a search bar feature with Javascript*


## What’s a Search Bar?

Okay, so we probably all know what a search bar is at this point but let me quickly explain. A search bar is an input box that appears on browsers, websites, and applications. It takes in a user’s search query and allows you to search for some type of information.

A [search engine](https://en.wikipedia.org/wiki/Search_engine) is the software system behind the scenes that handles searches on the web.

Did you know the [world wide web was entirely indexed by hand prior to the first web search engines?](https://en.wikipedia.org/wiki/Search_engine#:~:text=Prior%20to%20September%201993%2C%20the%20World%20Wide%20Web%20was%20entirely%20indexed%20by%20hand) I can probably write a whole article about the history of search engines but today we’re just going to be building a basic search bar for a web application.

## Building a Search Bar

To begin to create our search bar feature we should think about several concepts that go into a working version of it. We know that we’re going to need an empty search bar that takes in user input. We also need a way to capture that input and handle it to start finding what the user is searching for while they are typing it in. To find and render this data to the user we should store whatever type of data is being searched. In this case, we’re going to be working with a social app that allows users to search their list of friends out using a search bar.

To start building out our search bar we’re going to need to set up the HTML first to make it appear on our app. We can build out the basic structure to get something showing up on the page.

{% gist https://gist.github.com/CoderJay06/af2dcaeba66f59911a77a16c7c3b45f0 %}

Let’s check out what our search feature is looking like after setting everything up with HTML.

![Search bar feature with basic html](https://cdn-images-1.medium.com/max/2000/1*wuNraXafbt2DAKw88uHPBA.png)

Not the best-looking search app yet but we’re getting somewhere. We’ll give it some styling to make it more user-friendly.

{% gist https://gist.github.com/CoderJay06/1d03f7bfccf6cde091e2aac3d3f42d8e %}

This should give us a much nicer-looking app to work with.

![Search bar feature with css](https://cdn-images-1.medium.com/max/2000/1*Bq-7kc1W2D63a2DJ_atQZw.png)

Great! Now we can build the functionality to give our app the ability to search friends by typing in their names and having them render on the page. To do this we need to think about how we can handle the user event of typing in the search box using Javascript. We need to compare their search input to the names we have in our friend list. Let’s list the several steps we’ll take to implement this before writing out our Javascript.

* Grab `thesearchInput` and `name` elements from the DOM and store them in variables

* Add an event listener on the searchInput to listen for [keyup](https://developer.mozilla.org/en-US/docs/Web/API/Document/keyup_event) events

* Get user input once the event is triggered

* Search name elements and convert text to lowercase for case insensitive comparisons

* Compare names to the search query using the string method [includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes)

* Update name element styles to display matching names

{% gist https://gist.github.com/CoderJay06/df10b4047000cafdcc57d4d079faf76f %}

Now that we have our Javascript setup let’s test out our new search feature by starting to enter some input in the search bar.

![Finished search bar feature](https://cdn-images-1.medium.com/max/2000/1*TQyDwce_WH4wP2fUwY6-YQ.png)

It works! Now our search bar will give us the closest matching name results based on what letter characters we’re typing in.

## Summary

Search bars are a common feature of most web and mobile applications. They usually appear somewhere at the top corner of a web page and persist across the website for user convenience. Depending on the specifics of your application, search bars can be a great addition to it. For example, if your app holds a lot of information for the user it can greatly improve the user experience.