Bootstrap Searchable Dropdown Menu

Searchable Dropdown Menu

When we click on the dropdown button/arrow in this dropdown menu, an input field will pop up with all the dropdown items listing inside. If we type in any character, the corresponding items are searched out at once, and then we can click the needed option from the list.

How to make a searchable dropdown menu using Bootstrap?

Any button (class = “btn”) can be turned into a dropdown toggle with some markup changes. We have to add an input field of text type with the button to make it searchable. Some JavaScript functionalities are also added, as shown below in the examples:

Example:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
  
<style>
body{
    background-color: #dfe4ea; 
}
.btn {
  
  color: white;


  font-size: 16px;
  border: none;
  cursor: arrow;
}


.dropbtn:hover, .dropbtn:focus {
  background-color: rgb(78, 6, 18);
}


#myInput {
  box-sizing: border-box;
  background-image: url('searchicon.png');
  background-position: 14px 12px;
  background-repeat: no-repeat;
  font-size: 16px;
  padding: 14px 20px 12px 45px;
  border: none;
  border-bottom: 1px solid #ddd;
}


#myInput:focus {outline: 3px solid #ddd;}


.dropdown {
  position: relative;
  display: inline-block;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  overflow: auto;
  border: 1px solid #ddd;
  z-index: 1;
}


.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}


.dropdown a:hover {background-color: #ddd;}


.show {display: block;}
</style>
<title>Bootstrap Searchable Dropdown</title>
</head>
<body>
<div class="container">
<div class="row">   
<h2>This is an example to demonstrate a Searchable Dropdown Menu</h2>
<p>Click on the button below to see the dropdown menu. Type any character in the input field to search for a specific country.</p>
<div class="dropdown">
  <button onclick="myFunction()"  class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Default Dropdown Button</button>
  <div id="myDropdownmenu" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
        <a class="dropdown-item" href="#">Argentina</a>
        <a class="dropdown-item" href="#">Afganistan</a>
        <a class="dropdown-item" href="#">Brazil</a>
        <a class="dropdown-item" href="#">Sri Lanka</a>
        <a class="dropdown-item" href="#">Nepal</a>
        <a class="dropdown-item" href="#">India</a>
        <a class="dropdown-item" href="#">Japan</a>
        <a class="dropdown-item" href="#">Germany</a>
        <a class="dropdown-item" href="#">Switzerland</a>
        <a class="dropdown-item" href="#">Greece</a>
        <a class="dropdown-item" href="#">South Africa</a>
        <a class="dropdown-item" href="#">France</a>
        <a class="dropdown-item" href="#">Russia</a>
        <hr class="dropdown-divider">
        <a class="dropdown-item" href="#">Others</a>
  </div>
</div>
</div>
<script>
function myFunction() {
  document.getElementById("myDropdownmenu").classList.toggle("show");
}
function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  div = document.getElementById("myDropdownmenu");
  a = div.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    txtValue = a[i].textContent || a[i].innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
    } else {
      a[i].style.display = "none";
    }
  }
}
</script>
</body>
</html>

Output:

Bootstrap Searchable Dropdown Menu

On selecting the dropdown menu, it appears like the following:

Bootstrap Searchable Dropdown Menu

How to make a split searchable dropdown menu?

Consider the following code.

Example:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
  
<style>
body{
    background-color: #f1efe3; 
}
.btn {
  
  color: black;
 
  font-size: 16px;
  border: none;
  cursor: arrow;
}


.dropbtn:hover, .dropbtn:focus {
  background-color: rgb(226, 183, 89);
}


#myInput {
  box-sizing: border-box;
  background-image: url('searchicon.png');
  background-position: 14px 12px;
  background-repeat: no-repeat;
  font-size: 16px;
  padding: 14px 20px 12px 45px;
  border: none;
  border-bottom: 1px solid #ddd;
}


#myInput:focus {outline: 3px solid #ddd;}


.dropdown {
  position: relative;
  display: inline-block;
}


.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  overflow: auto;
  border: 1px solid #ddd;
  z-index: 1;
}


.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}


.dropdown a:hover {background-color: #ddd;}


.show {display: block;}
</style>
<title>Bootstrap Searchable Dropdown</title>
</head>
<body>
<div class="container">
<div class="row">   
        
<h2>This is an example to demonstrate a Split Searchable Dropdown Menu:</h2>
<p>Click on the toggle button below to see the dropdown menu. Type any character in the input field to search for a specific country.</p>


<div class="dropdown">
    <div class="btn-group"></div>
    <button type="button" class="btn btn-warning">Split Dropdown Button</button>
    <button type="button" onclick="myFunction()" class="btn btn-warning dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
      <span class="visually-hidden">Toggle Dropdown</span>
    </button>
    <div id="myDropdownmenu" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
   
        <a class="dropdown-item" href="#">Argentina</a>
        <a class="dropdown-item" href="#">Afganistan</a>
        <a class="dropdown-item" href="#">Brazil</a>
        <a class="dropdown-item" href="#">Sri Lanka</a>
        <a class="dropdown-item" href="#">Nepal</a>
        <a class="dropdown-item" href="#">India</a>
        <a class="dropdown-item" href="#">Japan</a>
        <a class="dropdown-item" href="#">Germany</a>
        <a class="dropdown-item" href="#">Switzerland</a>
        <a class="dropdown-item" href="#">Greece</a>
        <a class="dropdown-item" href="#">South Africa</a>
        <a class="dropdown-item" href="#">France</a>
        <a class="dropdown-item" href="#">Russia</a>
        <hr class="dropdown-divider">
        <a class="dropdown-item" href="#">Others</a>
      
  </div>
</div>


</div>
</div>
<script>


function myFunction() {
  document.getElementById("myDropdownmenu").classList.toggle("show");
}


function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  div = document.getElementById("myDropdownmenu");
  a = div.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    txtValue = a[i].textContent || a[i].innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
    } else {
      a[i].style.display = "none";
    }
  }
}
</script>


</body>
</html>

Output:

Bootstrap Searchable Dropdown Menu

After selecting the toggle button, the menu looks like this,

Bootstrap Searchable Dropdown Menu

Related Topics

Bootstrap List group

Bootstrap list group provides a group of a list of items. The most basic list is an unordered list. We can use <ul> element with .list-group class and <li> element with .lsit-group-item. Let us see...

3 minutes read.

Bootstrap 4 Grid Classes

Bootstrap 4 Grid Classes Extra-small Grid – In the extra-small grid system class, when you create the grid (columns), the columns will not be stacked on top of each other on...

8 minutes read.

Bootstrap Buttons Groups

Bootstrap 4 Button Group Bootstrap 4 provides a feature button group that helps in creating a series of buttons together in a group. Basic Button Group  The .btn-group class is used within the <div> tag to create a...

6 minutes read.

Bootstrap Validation Example

Validation We can provide valuable and actionable feedback to the users through an HTML form validation. It happens via custom styles or behaviors of default browsers and JavaScript. How does validation work...

4 minutes read.

Bootstrap 4 Layout

Containers Containers are the essential elements of the bootstrap layout. It is required when we are using a bootstrap grid system. Containers can also be nested, but most layouts do not need nested containers. Bootstrap has...

3 minutes read.

Bootstrap 4 Form

Bootstrap 4 Forms Bootstrap 4 provides forms, which are one of the essential components for web pages and web applications. It is an input-based component, used to collect user data with the help of...

8 minutes read.

Bootstrap 4 Modal

Bootstrap 4 Modal The Modal is a component of Bootstrap 4, which is a dialog box or pop-up window. The modals are used to provide information or warning to the user, such as...

18 minutes read.

Bootstrap Notification Bubble

What is a notification bubble? Bubbles are built into the Notification system. They float on top of the website or the button to indicate pending notifications. Bubbles can be expanded to reveal...

5 minutes read.

Bootstrap 4 Progress Bar

Bootstrap Progress Bar Bootstrap 4 offers progress bars that are used to represent the progress of tasks such as downloading a file, software installation, etc. on the computer. It represents the percentage of the...

9 minutes read.

Bootstrap Protocol (BOOTP)

In the following article, we will learn about the Bootstrap Protocol and how it maintains contracts among linked devices on webwork. What is Bootstrap Protocol? The Bootstrap Protocol is a webwork contract...

3 minutes read.

Bootstrap 4 Tutorial

What is Bootstrap? Bootstrap is a CSS (Cascading Style Sheets) framework, which is used to develop a responsive website. It is an open-source and mobile-first front-end framework. It has design templates that are based...

4 minutes read.

Bootstrap 4 Scrollspy

Bootstrap 4 Scrollspy Bootstrap 4 offers a scrollspy component that is used to make the website more attractive and eye-catching. Basically, the scrollspy is used on the single-page website. The scrollspy highlights the nav...

5 minutes read.

Bootstrap 4 navbar

A navigation navbar or navbar uses in every website to make it more user-friendly. Navigating through the website is easier, and the user can instantly search for the topic of...

8 minutes read.

Bootstrap 4 Toast

Bootstrap 4 Toast Bootstrap 4 offers a Toast component, which is quite similar to the alert box and pushes notifications. The toast is visible on the screen for a few seconds when any...

5 minutes read.

Bootstrap 4 Icons

Bootstrap 4 Icons Bootstrap 3 contains the default icon library. But in bootstrap 4, you have to include the external icon library by yourself. Many free icon libraries are available such...

2 minutes read.

Bootstrap 4 Card

In Bootstrap 4, a card is a bordered box with padding around its content. It has options for headers, footers, colors, content, etc. The card provides the link, title, image,...

8 minutes read.

Bootstrap 4 Tooltip

Bootstrap 4 Tooltip Tooltip Bootstrap 4 offers a tooltip component, a small popup box that appears when you move the cursor over an element such as buttons or any highlighted element. It...

3 minutes read.

Bootstrap 4 Popover

Bootstrap 4 Popover Popover A popover is similar to the tooltip. The only difference between tooltip and Popover is that in the tooltip, the popup box appears when you move the cursor...

6 minutes read.

Difference between bootstrap.css and bootstrap-theme.css

The "bootstrap.css" uses to create a website with pre-existing designs and context. It uses basic Bootstrap classes like rows and columns with a CSS styling tool. The website is styled...

5 minutes read.

Bootstrap 4 Media Objects

Bootstrap 4 Media Objects Bootstrap 4 offers a media object component that aligns the media objects and the content. The media object is used to create a repetitive and complex component...

7 minutes read.