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 Input groups

Bootstrap 4 Input groups The input group is a component of Bootstrap 4, which is used to create interactive and stylish form controls. With the help of input groups, you can easily customize the input...

8 minutes read.

Dropdown & Dropup

Bootstrap Dropdowns The bootstrap dropdown menu is used to create a toggleable menu. It provides users to choose one value from a predefined list. Before creating dropdown menu, we have to know...

3 minutes read.

Bootstrap 4 Pagination

Bootstrap 4 Pagination Bootstrap pagination is used to show the existence of a series of web pages of the website. It allows navigation on multiple pages of a website. In pagination, there is a block...

9 minutes read.

Bootstrap Glyphicons

Bootstrap Glyphicon is used to create different type of glyphicons It is used in the web project. It provides 260 Glyphicons from the Glyphivons Halflings set. There are various examples of...

1 minute read.

Environment Setup

How to use Bootstrap? We can use bootstrap in our project either by downloading or by providing link of the library files. 1) Download Bootstrap files To download files, visit official site of bootstrap www.getbootstrap.com/download/ and...

1 minute read.

Bootstrap 4 Grid System

Bootstrap 4 Grid System The bootstrap 4 grid system is used to create a responsive website layout. The bootstrap 4 offers a mobile-first flexbox grid system that allows you to divide...

4 minutes read.

Bootstrap 4 Spinners

Bootstrap 4 offers spinner is a loading indicator that is used to display the loading state of any component or web page. The spinner is also known as a loader...

7 minutes read.

Bootstrap Images

Bootstrap 4 Images Bootstrap provides several classes for images to make them responsive and also helps to give some style to the images. Image Shapes Rounded Corners Bootstrap 4 offers .rounded class that makes the corners...

5 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 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 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 Typography

Bootstrap provides simple and easily customized typography for headings, body text, lists, and many more. Headline <h1> to <h6> By default, Bootstrap has HTML headings (<h1> to <h6>) that are given below: Example <h1>h1. Bootstrap heading</h1>   <h2>h2. Bootstrap heading</h2>   <h3>h3. Bootstrap heading</h3>   <h4>h4. Bootstrap heading</h4>   <h5>h5. Bootstrap heading</h5>   <h6>h6. Bootstrap heading</h6> Try Now ←...

1 minute read.

Bootstrap Contact Us page

What is a “Contact Us” page? A “Contact Us” page is a web page available on the websites of various companies, organisations etc., for its users to contact the organisation or...

5 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 Panels

A bootstrap panel is used to create bordered box with some padding around its content. It is created with the .panels classes, and .panel-body classes. There are following classes of the panel: Panel header Panel...

4 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.

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 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 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 4 Content

Typography Typography is used for styling and formatting the content. It also helps in creating customized headings, sub-headings, lists, paragraphs, etc. Headings Bootstrap 4 offers HTML heading <h1> to <h6> that has bolder...

8 minutes read.