Warm tip: This article is reproduced from serverfault.com, please click

On select to show the selected dropdown item

发布于 2020-11-29 09:30:58

I have a bootstrap code for the search bar with a dropdown list but I want to display the option upon selection one from the dropdown list. Currently, it shows "All" and even you click on the dropdown and select any of the options it does not work. Anyone on it using JavaScript or jQuery to make it work

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap 4 Search Box With Dropdown List</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

  <body>
    <div class="search-box col-md-5">
      <form action="">
        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <button class="btn btn-light dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All</button>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Category One</a>
              <a class="dropdown-item" href="#">Category Two</a>
              <a class="dropdown-item" href="#">Category Three</a>
              <div role="separator" class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Separated Category</a>
            </div>
          </div>
          <input type="text" class="form-control" aria-label="Search input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-success" type="button">Search</button>
          </div>
        </div>
      </form>
    </div>
  </body>

</html>
Questioner
Omer Farooq Butt
Viewed
0
Alen.Toma 2020-11-29 17:49:30

You could simple add click event on

dropdown-item

see below

$('.dropdown-menu .dropdown-item').click(function(){
     var value = $(this).text();
     console.log(value)
     // Update the value and show it to the user
     $(this).parent().parent().find(".dropdown-toggle").html(value);

});
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap 4 Search Box With Dropdown List</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

  <body>
    <div class="search-box col-md-5">
      <form action="">
        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <button class="btn btn-light dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All</button>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Category One</a>
              <a class="dropdown-item" href="#">Category Two</a>
              <a class="dropdown-item" href="#">Category Three</a>
              <div role="separator" class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Separated Category</a>
            </div>
          </div>
          <input type="text" class="form-control" aria-label="Search input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-success" type="button">Search</button>
          </div>
        </div>
      </form>
    </div>
  </body>

</html>