1

this is an extended question I encountered after my previous question.

The problem lies when someone clicked on the arrow to show the advanced searches, it drops down the form as expected. However, the moment when someone clicked on the select drop down, the whole dropdown menu would then collapse on it's own.

I've been trying to find out what element/trigger is causing this issue and see if there's a way to work around this, but I have no progress at all

Here's what I've observed so far

  1. [Expected] If the user clicked outside of the drop menu area, the menu collapses
  2. [Unexpected] If the user clicked any of the whitespace within the drop menu, the menu still collapses
  3. [Expected] if user clicked on the text field, checkbox, radiobox, textarea, they are supposed to be able to type and enter required texts.
  4. [Unexpected] If the user clicked a select menu, the dropmenu collapses instantly

I have dropped a sample code down below to demonstrate the question I have.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <div class="container">
      <div class="row">
        <div class="col-12">
          <div class="input-group dropdown">
            <input type="text" class="form-control" id="project_search" placeholder="search ...">
            <div class="input-group-append">
              <div class="btn-group">
                <button class="btn bg-primary text-white" type="button">Search</button>
                <button class="btn bg-primary text-white dropdown-toggle dropdown-toggle-split" id="searchAdvancedFilter" data-toggle="dropdown" data-boundary="window" aria-haspopup="true" aria-expanded="false">
                  <span class="caret"></span>
                  <span class="sr-only">Toggle Dropdown</span>
                </button>
                <div class="dropdown-menu dropdown-menu-right p-3 w-100" aria-labelledby="searchAdvancedFilter">
                  <h6 class="dropdown-header">Advanced Settings</h6>
                  <div class="dropdown-divider"></div>
                  <div class="form-group">
                    <label for="option">Option Header</label>
                    <select class="form-control" id="option" name="option">
                      <option>Pick an option</option>
                      <option value=1>Option 1</option>
                      <option value=2>Option 2</option>
                    </select>
                  </div>
                  <div class="form-group">
                    <label for="inputtext">Input Header</label>
                    <input type="text" class="form-control" id="inputtext" name="inputtext">
                  </div>
                  <div class="form-group">
                    <label for="inputcb">Checkbox Header</label>
                    <input type="checkbox" class="form-control" id="inputcb" name="inputcb">
                  </div>
                  <div class="form-group">
                    <label for="inputradio">Radio Header</label>
                    <input type="radio" class="form-control" id="inputradio" name="inputradio">
                  </div>
                  <div class="form-group">
                    <label for="ta">TextArea Header</label>
                    <textarea class="form-control" id="ta" name="ta"></textarea>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
Willy_Sunny
  • 199
  • 3
  • 14

1 Answers1

4

Bootstrap Dropdown menus will always close on click, except for when a <form> is used. The way to prevent it from closing on click is to use a <form> element inside dropdown...

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="container">
<div class="row">
    <div class="col-12">
        <div class="input-group dropdown">
            <input type="text" class="form-control" id="project_search" placeholder="search ...">
            <div class="input-group-append">
                <div class="btn-group">
                    <button class="btn bg-primary text-white" type="button">Search</button>
                    <button class="btn bg-primary text-white dropdown-toggle dropdown-toggle-split" id="searchAdvancedFilter" data-toggle="dropdown" data-boundary="window" aria-haspopup="true" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <div class="dropdown-menu dropdown-menu-right p-3 w-100" aria-labelledby="searchAdvancedFilter">
                        <h6 class="dropdown-header">Advanced Settings</h6>
                        <div class="dropdown-divider"></div>
                        <form>
                        <div class="form-group">
                            <label for="option">Option Header</label>
                            <select class="form-control" id="option" name="option">
                                <option>Pick an option</option>
                                <option value="1">Option 1</option>
                                <option value="2">Option 2</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="inputtext">Input Header</label>
                            <input type="text" class="form-control" id="inputtext" name="inputtext">
                        </div>
                        <div class="form-group">
                            <label for="inputcb">Checkbox Header</label>
                            <input type="checkbox" class="form-control" id="inputcb" name="inputcb">
                        </div>
                        <div class="form-group">
                            <label for="inputradio">Radio Header</label>
                            <input type="radio" class="form-control" id="inputradio" name="inputradio">
                        </div>
                        <div class="form-group">
                            <label for="ta">TextArea Header</label>
                            <textarea class="form-control" id="ta" name="ta"></textarea>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

https://www.codeply.com/go/18ACVL2uzn


Another option is to use the jQuery stopPropagation() method: https://www.codeply.com/go/4dVvDDpChW

$('.dropdown-menu').click(function(e) {
    e.stopPropagation();
});
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • Question, then if I were to move the
    before the `.dropdown-menu` class, and place it for example, where the `.row` class at the beginning and encapsulate everything, (since I'd want the standard search input field to be included), the same issue would occur.
    – Willy_Sunny Apr 13 '18 at 19:25
  • Yes, the form has to be inside the dropdown-menu. It can also be solved with jQuery. – Carol Skelly Apr 13 '18 at 20:07
  • Is there anyway to stop the bootstrap to close on click? and only close when we tell it to? – Willy_Sunny Apr 22 '18 at 20:42
  • Yes, you'd use [jQuery instead of a
    ](https://www.codeply.com/go/4dVvDDpChW) as explained in [other answers](https://stackoverflow.com/questions/26639346/prevent-bootstrap-dropdown-from-closing-on-clicks).
    – Carol Skelly Apr 23 '18 at 11:13