Replace bootstrap downdrop default text with selected option's text

Viewed 38

How can I make the bootstrap dropdown selection replace the default text upon user selection? I can only find advice for doing this with selection boxes, and not Bootstrap 4 dropdowns specifically.

When the user selects a dropdown option, I want the "Please Select" it starts with to be updated. Currently when the user makes a choice the dropdown menu just gets hidden again.

        <div class="dropdown">
                <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Please Select //here is where I would like to be updated
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
                  <button class="dropdown-item" type="button">Action</button>
                  <button class="dropdown-item" type="button">Another action</button>
                  <button class="dropdown-item" type="button">Something else here</button>
                </div>
       </div>
1 Answers

A dropdown isn't really suited for this. Perhaps you should consider using an input select instead?

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container">
  <div class="form-group">
    <label for="sel1">Please Select</label>
    <select class="form-control" id="sel1">
      <option value="" disabled selected>Select your option</option>
      <option>Action</option>
      <option>Another action</option>
      <option>Something else here</option>
    </select>
  </div>
</div>

Related