"Trouble arises as bootstrap-select fails to function properly within a Bootstrap dropdown menu

I'm attempting to incorporate a multiselect input from the bootstrap-select Library into a Bootstrap dropdown menu. The issue arises when I open the dropdown menu and click on the multiselect input, causing the menu to close and preventing me from using the input within the dropdown menu.

Even if I place the multiselect input outside of the dropdown menu, the same issue occurs. How can I ensure that when I interact with the multiselect input inside the dropdown, the menu remains open so I can freely select options?

My code can be found here (JSFiddle):

<div class="dropdown mb-4 text-dark" id="myDD">
    <a class="btn dropdown-toggle text-muted btn-block btn-grey py-2 font-weight-bold "
       style="color: black !important; font-size: .8em;" href="#" role="button"
       id="dropdownMenuLink"
       data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
       data-display="static">
        <i class="fas fa-sliders-h mr-2"></i> Menu
    </a>
    <div class="dropdown-menu bg-transparent border-0 mt-2"
         aria-labelledby="dropdownMenuLink"
         style="position: relative; float: none;">
        <form>
            <div class="input-group mb-3">
                <label for="inputGroupSelect05"
                       class="text-dark d-block w-100 mb-1">Input text</label>
                <input type="text" class="form-control form-control-sm" placeholder="text input">
            </div>

            <div class="input-group mb-3 multi-select ">
                <label for="inputGroupSelect02" class="text-dark d-block w-100 mb-1">Click causes close of dropdown</label>
                <select style="color: #495057 !important;" class="w-100" multiple data-selected-text-format="count" data-style="custom-select custom-select-sm" id="test-select">
                    <option>1</option>
                    <option>2</option>
                    <option selected>3</option>
                    <option selected>4</option>
                    <option selected>5</option>
                </select>
            </div>
        </form>
    </div>
</div>

I've experimented with various JQuery solutions for similar issues, but none seem to solve the problem. Does anyone have any suggestions on how to resolve this?

Answer №1

When opening your select-box, you may encounter a situation where the menu hides. To prevent this, one approach could be to manually control the opening and closing of the menu. When the menu is clicked, you can add or remove the class show from both dropdown and dropdown-menu.

Here is a Demo Code:

$(document).ready(function() {
  $('#test-select').selectpicker();
  $('#test-select2').selectpicker();
// When the dropdown toggle is clicked 
  $('#myDD > a.dropdown-toggle').on('click', function(event) {
    $(this).parent().toggleClass('show')//add or remove class
    $(this).attr('aria-expanded', $(this).attr('aria-expanded') == 'false' ? 'true' : 'false'); //add true or false
    $("div[aria-labelledby=" + $(this).attr('id') + "]").toggleClass('show') //add class or remove
  });

  $('body').on('click', function(e) {
  // Check if the click occurred outside the `myDD` tag, if yes, hide the menu
    if (!$('#myDD').is(e.target) &&
      $('#myDD').has(e.target).length === 0 &&
      $('.show').has(e.target).length === 0
    ) {
     // Remove classes and add attributes 
      $('#myDD').removeClass('show')
      $('#myDD > a').attr('aria-expanded', 'false');
      $("#myDD").children('div.dropdown-menu').removeClass('show')
    }
  });
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="462429293235323427366b35232a2325320677687775687772">[email protected]</a>/dist/css/bootstrap-select.min.css">
<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>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a1acacb7b0b7b1a2b3eeb0a6afa6a0b783f2edf2f0edf2f7">[email protected]</a>/dist/js/bootstrap-select.min.js"></script>

<div class="dropdown mb-4 text-dark" id="myDD">
  <a class="btn dropdown-toggle text-muted btn-block btn-grey py-2 font-weight-bold " style="color: black !important; font-size: .8em;" href="#" role="button" id="dropdownMenuLink" aria-haspopup="true" aria-expanded="false" data-display="static">
    <i class="fas fa-sliders-h mr-2"></i> Menu
  </a>
  <div class="dropdown-menu bg-transparent border-0 mt-2" aria-labelledby="dropdownMenuLink" style="position: relative; float: none;">
    <form>
      <div class="input-group mb-3">
        <label for="inputGroupSelect05" class="text-dark d-block w-100 mb-1">Input text</label>
        <input type="text" class="form-control form-control-sm" placeholder="text input">
      </div>

      <div class="input-group mb-3 multi-select ">
        <label for="inputGroupSelect02" class="text-dark d-block w-100 mb-1">Click causes close of dropdown</label>
        <select style="color: #495057 !important;" class="w-100" multiple data-selected-text-format="count" data-style="custom-select custom-select-sm" id="test-select">
          <option>1</option>
          <option>2</option>
          <option selected>3</option>
          <option selected>4</option>
          <option selected>5</option>
        </select>
      </div>

    </form>

  </div>
</div>

<div class="input-group mb-3 multi-select ">
  <label for="inputGroupSelect02" class="text-dark d-block w-100 mb-1">Click causes close of dropdown if open</label>
  <select style="color: #495057 !important;" class="w-100" multiple data-selected-text-format="count" data-style="custom-select custom-select-sm" id="test-select2">
    <option>1</option>
    <option>2</option>
    <option selected>3</option>
    <option selected>4</option>
    <option selected>5</option>
  </select>
</div>

Answer №2

If you are using Bootstrap 5, please note that Bootstrap select no longer supports it. Instead, you can use the multiple-select library. Check out the examples here.

Alternatively, you can upgrade Bootstrap-select to a version that supports Bootstrap 5. The current compatible version is v1.14.0-beta2. Swati has also removed the data-toggle for you, and any other data-* attributes should be changed to data-bs-* for Bootstrap 5. More information can be found here; note that this change does not apply to Bootstrap-select attributes, such as data-live-search.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to display tip boxes for content that is added dynamically?

One of the challenges with my web page is that dynamically added content does not display tipboxes, unlike items present since the page load. I utilize the tipbox tool available at http://code.google.com/p/jquery-tipbox/downloads/list. To illustrate the i ...

Is it possible to display the content below the row of the clicked element when it is selected?

I am currently working on building a team page similar to the layout at My goal is to create a row of elements that float or display inline, with hidden content revealed beneath each selected element, pushing subsequent rows further down. Unfortunately, m ...

Looking for assistance in creating a scrollable div element

I'm looking to create a scrollable div similar to the one shown on the page linked below for a large secondary navbar. This navbar will expand to the bottom of the page after the title and primary navbar. So when I scroll down the page, the title and ...

What is the maximum character limit for the JQuery validation plugin?

Currently, I am utilizing the JQuery validation plugin in my project. My goal is to set a maxlength for one of the fields. Here's an example of how it can be done by defining rules externally: rules: { Message: { required: false, maxLe ...

What is the best way to present a Python list in individual lines on an HTML webpage?

This might be a simple question, and I apologize if I haven't explained it clearly. This is my first time working with HTML, and I'm using a given program that I don't fully comprehend. In the python program, app.py, I read a list of string ...

"Exploring JSON data with jQuery: A guide to efficient search techniques

I have a local file containing JSON data which I successfully loaded using jQuery. My current task is to specifically find the pId with the value of "foo1". The JSON data { "1":{ "id": "one", "pId": "foo1", "cId": "bar1" }, "2":{ ...

What is the best way to incorporate a variety of colors into a single element?

Using a combination of colors in one element I'm looking for ways to incorporate multiple colors into a single element effectively. My initial attempt with the color mix function didn't work as expected, so I need new ideas and techniques on how ...

I am looking for a setup where a checkbox triggers the opening of the next accordion

I am encountering a problem with the bootstrap accordion feature. I am looking to have the first accordion nested inside a checkbox, so that when the checkbox is clicked, the next accordion automatically opens. If the checkbox is not clicked, the next ac ...

Utilizing document.getElementById().value in hidden fields for asp.net web pages does not function correctly

Recently, I developed a basic website using ASP.NET Web Pages with Razor markup. The site includes a form with two hidden fields: latitude and longitude. My goal was to utilize the HTML5 Geolocation API in my JavaScript script to obtain the user's loc ...

Bootstrap 3 Full-Width Responsive Image Overlapping

I have designed a grid layout with col-md-7 on the left side and col-md-5 on the right side. The images in the col-md-5 are responsive and have a full-width of col-md-12. <div class="container"> <div class="row"> <div class="col ...

What is the best way to stack columns on small screens in a single row?

While utilizing the default bootstrap col classes, I noticed that when I reduce the grid to mobile size, the columns span the entire width of the row. This results in a long vertical list of items on mobile devices, even though there is sufficient space on ...

Error: The database connection is currently down

Hey there! I'm currently working on a school project and running into some issues with inserting data into the database. I can't figure out what I'm doing wrong. There are no errors showing up, but for some reason, the information I input i ...

Integration issues in RetinaJs and jChartFX collaboration

I am currently utilizing jChartFX for charting purposes on my website. I have included the following line of code to bring in the footer: <?php include $_SERVER["DOCUMENT_ROOT"].'/footer.php' ?> The footer contains a script reference like ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...

Initiating a YouTube video with a click on its thumbnail image - a jQuery tutorial

I am currently working on code that successfully displays YouTube videos. However, I would like the video to start playing when the cover image is clicked. How can I achieve this functionality? Thank you for your help! <div id="video" style="display: ...

Guide on how to modify the color of a single row within a table with just a click

My table structure is as follows: <table> <tr> <td>A1</td> <td>A2</td> <td>A3</td> <td>A4</td> </tr> <tr> ...

Retrieve the JSON response from the server and store it in variables using jQuery's AJAX function with the `done

I am trying to retrieve a JSON response from the server upon clicking a button and then parse it into a div. However, I am struggling with how to accomplish this. <button type="submit" id="btPay" name="btPay"> Go for Pay ...

Drop Down Automation with Selenium IDE

Currently in the process of automating a User Acceptance Testing (UAT) for a software project at my company. I've completed all the typical tasks like recording actions and clicking on buttons, but ran into an issue with a drop-down menu. The problem ...

Creating an animated background slide presentation

After creating a button group, I wanted the background of the previous or next button to move/slide to the selected one when the user clicks on it. I achieved this effect using pure CSS and simply adding or removing the 'active' class with jQuery ...

Creating a text input that spans the full width of the form in a Bootstrap

I'm having trouble creating a text box that spans the entire width of my container. <div class="row"> <div class="col-md-12"> <form class="form-inline" role="form"> <input type="text" class= ...