A responsive Bootstrap Search Box that really searches
Tags: bootstrap, javascript, webdev, html
Published: 2021-04-20 12:46:00

There are plenty of search box examples on the Internet, but all of them are actually static components that don't perform their crucial action - searching.

In this simple and short tutorial, I'll show you how to achieve the desired result and how to create a fully functional search box, that really searches.

________

#### Note: If you want to learn more about the basics of Bootstrap Search have a look at our [Search Docs](https://mdbootstrap.com/docs/standard/forms/search/).

________


In this tutorial, I will use Bootstrap 5 which is the newest version of the Bootstrap.

The search functionality will be written in plain JavaScript, with no dependencies.

I will also use a free UI KIT [Material Design for Bootstrap](https://mdbootstrap.com/docs/standard/), to apply Material Design look to the project.

__________


## Step 1

Go to the [installation page](https://mdbootstrap.com/docs/standard/getting-started/installation/) and download Material Design for Bootstrap. It's free, MIT licensed and you can freely use it in any projects - also commercial.

Unzip the package and open it in your favorite code editor. I recommend Visual Studio Code together with our extension [VSC code snippets for MDB](https://mdbootstrap.com/docs/standard/getting-started/vsc-snippets/).


____________



## Step 2

I am adding a simple grid to provide a proper margin and card inside of the grid. This step is not strictly related to the search box and it's used only to improve the visual effect of this small project.

It's fully optional and you can skip this step if you care only about search functionality.

```html
<div class="container my-5">
  <div class="row d-flex justify-content-center">
    <div class="col-lg-6">
      <div class="card">
        <div class="card-body">
          <!-- Here goes the search box -->

          <!-- /Here goes the search box -->
        </div>
      </div>
    </div>
  </div>
</div>
```


____________



## Step 3

From the Search Docs copy the example of the [Search with Icon](https://mdbootstrap.com/docs/standard/forms/search/#subsection-search-with-icon).

This example will provide us a necessary UI, to which we will add functionality in JavaScript.

```html
<div class="container my-5">
  <div class="row d-flex justify-content-center">
    <div class="col-lg-6">
      <div class="card">
        <div class="card-body">
          <!-- Search box -->
          <div class="input-group rounded">
            <input type="search" class="form-control rounded" placeholder="Search" aria-label="Search" aria-describedby="search-addon">
            <span class="input-group-text border-0" id="search-addon">
              <i class="fas fa-search"></i>
            </span>
          </div>
          <!--/ Search box -->
        </div>
      </div>
    </div>
  </div>
</div>
```

____________



## Step 4

Set a proper ID to the input and to the label. We will use this ID in our JavaScript to get the Search component and make it actually searches.

I am also adding `.mb-4` class to the `.input-group` element to provide some margin-bottom and make a space between the search box and the list of items that we will place below.

```html

<div class="container my-5">
  <div class="row d-flex justify-content-center">
    <div class="col-lg-6">
      <div class="card">
        <div class="card-body">
          <!-- Search box -->
          <div class="input-group rounded mb-4">
            <input type="search" id="search-box" class="form-control rounded" placeholder="Search for cars.." aria-label="Search" aria-describedby="search-box">
            <span class="input-group-text border-0" id="search-box">
              <i class="fas fa-search"></i>
            </span>
          </div>
          <!-- / Search box -->
        </div>
      </div>
    </div>
  </div>
</div>
```

____________


## Step 5

Let's add a list of items that we will search through.

From the [List Group Docs](https://mdbootstrap.com/docs/standard/components/list-group/#section-basic-example) copy the basic example. Then place a link inside each of the `li` elements with a car brand inside.

```html
<div class="container my-5">
  <div class="row d-flex justify-content-center">
    <div class="col-lg-6">
      <div class="card">
        <div class="card-body">
          <!-- Search box -->
          <div class="input-group rounded mb-4">
            <input type="search" id="search-box" class="form-control rounded" placeholder="Search for cars.." aria-label="Search" aria-describedby="search-box">
            <span class="input-group-text border-0" id="search-box">
              <i class="fas fa-search"></i>
            </span>
          </div>
          <!-- / Search box -->

          <!-- List -->
          <ul class="list-group" id="car-list">
            <li class="list-group-item">
              <a href="#">Tesla</a>
            </li>
            <li class="list-group-item">
              <a href="#">Honda</a>
            </li>
            <li class="list-group-item">
              <a href="#">Ford</a>
            </li>
            <li class="list-group-item">
              <a href="#">Volkswagen </a>
            </li>
            <li class="list-group-item">
              <a href="#">BMW </a>
            </li>
            <li class="list-group-item">
              <a href="#">Audi </a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</div>
```


____________


## Step 6

Our UI is ready and now we can take care of JavaScript.

We get the search box through an ID and add a keyup event which will trigger the function every time the user type anything in the search input.

```javascript
document
// Get a search box
.getElementById("search-box")
// Add keyup event
.addEventListener("keyup", function () {

  // Search functionality

});
```

____________


## Step 7

Now we declare necessary variables and assign values to them.

```javascript
document
// Get a search box
.getElementById("search-box")
// Add keyup event
.addEventListener("keyup", function () {

  // Search functionality

  // Decalare a variables
  let input, filter, ul, li, a, i, txtValue;

  // Assign values
  input = document.getElementById("search-box");
  filter = input.value.toUpperCase();
  ul = document.getElementById("car-list");
  li = ul.getElementsByTagName("li");

});
```

____________


## Step 8

Add the end we create `for Loop` to loop through all list items, and hide those who don't match the search query.

```javascript
document
// Get a search box
.getElementById("search-box")
// Add keyup event
.addEventListener("keyup", function () {

  // Search functionality

  // Decalare a variables
  let input, filter, ul, li, a, i, txtValue;

  // Assign values
  input = document.getElementById("search-box");
  filter = input.value.toUpperCase();
  ul = document.getElementById("car-list");
  li = ul.getElementsByTagName("li");

  // Loop through all list items, and hide those who don't match the search query
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    txtValue = a.textContent || a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }

});
```

____________

### That's it. Our search functionality is ready.

See the final result [here](https://mdbootstrap.com/snippets/standard/ascensus/2890900)