简体   繁体   中英

My dropdown is not working using bootstrap

I create a dropdown which is the drop down will be triggered by a button from the search bar and underneath the search bar the drop down content will be deploy. Though my code wont work

<div class="col-8">
  <div class="input-group mb-3">
    <div class="input-group-prepend" id="button-addon3">
      <div class="dropdown">
        <button class="btn btn-outline-secondary " type="button" style="color:black;background-color: darkcyan">Search</button>
        <div id="myDropdown" class="dropdown-content">
          <a href="#home">Home</a>
          <a href="#about">About</a>
          <a href="#contact">Contact</a>
        </div>
      </div>
      <button class="btn btn-outline-secondary" type="button" style="color:black;background-color: darkcyan" onCLick="myFunction()">Filter</button>
    </div>
    <input type="text" class="form-control" placeholder="Search Reports Here" aria-label="Example text with two button addons" aria-describedby="button-addon3">
  </div>
</div>
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
    }
    window.onclick = function(event) {
        if (!event.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
            }
            }
        }
    }

Hi if you are using bootstrap you can make use of bootstrap dropdown itself please kindly use the below code no need of writing script as well in this. :)

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</button>
    <div class="dropdown-menu">
      <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 role="separator" class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Separated link</a>
    </div>
<button class="btn btn-outline-secondary" type="button">Filter</button>
  </div>
  <input type="text" class="form-control" aria-label="Text input with dropdown button">
</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM