Search code examples
javascriptdrop-down-menujirabootstrap-4search-box

How to made a searchable drop down


So i made a dropdown using bootstrapp.

The code i took here : http://getbootstrap.com/docs/4.0/components/dropdowns/

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

Now i want to add a search box inside the dropdown list , so that people can type in and it will shorten to a more proper dropdown list.

I try to find many ways , some of them using external plugin like Select2, however , they need a dropdown coded by using <select> and <option> tag . In contrast , my code use button and built-in dropdown class of Bootstrapp.

Anyone please help me .


Solution

  • thanks guys for helping , i have found the solution . ill post here for anyone who needs

    function searchBox() {
        var input, filter, ul, li, a, i;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        ul = document.getElementById("myUL");
        li = ul.getElementsByTagName("li");
        for (i = 0; i < li.length; i++) {
            a = li[i].getElementsByTagName("a")[0];
            if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
                li[i].style.display = "";
            } else {
                li[i].style.display = "none";
    
            }
        }
    }