Easy Tutorial
❮ Front End Interview A Few Important Points Of Knowledge Android Tutorial Webview Download File ❯

JavaScript/CSS Dropdown Menu Search Functionality

Category Programming Techniques

In this section, we will learn how to implement search or filter functionality for a dropdown menu using JS/CSS.

First, let's see the effect as follows:

View the live demo: https://c.tutorialpro.org/codedemo/6229/

Basic HTML Code

Example

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown Menu</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#about">Google</a>
    <a href="#base">tutorialpro</a>
    <a href="#blog">Taobao</a>
    <a href="#contact">Wiki</a>
    <a href="#custom">Zhihu</a>
    <a href="#support">Tmall</a>
    <a href="#tools">Weibo</a>
  </div>
</div>

The following are the styles for the search box and the dropdown menu:

Example

/* Dropdown button */
.dropbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* Hover and focus style for the dropdown button */
.dropbtn:hover, .dropbtn:focus {
  background-color: #3e8e41;
}

/* Search box */
#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;
}

/* Style for the search box when it is focused */
#myInput:focus {outline: 3px solid #ddd;}

/* Container <div> - positioning the dropdown menu */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown menu content (hidden by default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  border: 1px solid #ddd;
  z-index: 1;
}

/* Dropdown menu link style */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Hover style for the links */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (add .dropdown-content class with JS) */
.show {display:block;}

The following is the JavaScript code for the search box and the dropdown menu:

Example

/* Function to toggle the display of the dropdown menu when the button is clicked */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

/* Search functionality */
function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  div = document.getElementById("myDropdown");
  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";
    }
  }
}

**Share My Notes

Cancel

-

-

-

❮ Front End Interview A Few Important Points Of Knowledge Android Tutorial Webview Download File ❯