0

Is it possible to create a multi-select checkbox dropdown like this in Bootstrap, HTML and JavaScript.

I have tried I couldn't able to achieve it. I'm new to JavaScript as well However I'm just exploring the JS and make it to work but this seems really challenging for me. I don't know how to implement JS script for this.

For instance: If the user selects the countries in the multiselect checkbox I want it to show it all countries name instead of Choose option.

code:

        <div>Country</div>
        <div class="row" name="country_checkbox" id="id_row">
          <div class="dropdown">
            <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">
              <span id="selected">Choose option</span><span class="caret"></span></a>


          <ul id="id_country">
            <li><label for="id_country_0"><input type="checkbox" name="country" value="US"
                  placeholder="Select Country" id="id_country_0" onclick="filter_type();">
                US</label>

            </li>
            <li><label for="id_country_1"><input type="checkbox" name="country" value="India"
                  placeholder="Select Country" id="id_country_1" onclick="filter_type();">
                India</label>

            </li>
            <li><label for="id_country_2"><input type="checkbox" name="country" value="Japan"
                  placeholder="Select Country" id="id_country_2" onclick="filter_type();">
                Japan</label>

            </li>
            <li><label for="id_country_3"><input type="checkbox" name="country" value="UK"
                  placeholder="Select Country" id="id_country_3" onclick="filter_type();">
                UK</label>
            </li>

          </ul>
        </div>
     

js fiddle here: http://jsfiddle.net/shalman44/92drs7ax/3/

imran_44
  • 171
  • 6
  • 18

2 Answers2

0

Since you are already using bootstrap, you might aswell want to use jQuery. There is a plugin called bootstrap-select for achieving a multi select like in your example, where the syntax is as follows:

<select class="selectpicker" multiple>
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

$(function () {
    $('select').selectpicker();
});

You might also want to have a look at this question.

PaulS
  • 850
  • 3
  • 17
  • I have multiple checkboxes how could I possibly implement it. though I wanted to implement in bootstrap 5 – imran_44 Oct 25 '21 at 09:46
  • @imran_44 you can just create multiple – PaulS Oct 25 '21 at 09:49
  • I have implemented it but I have on click JS functionality to the each option value in the dropdown list. how could I able to achieve it. I have the JS functionality check boxes which are working properly here u can see the fiddle base on the user selection the particular matching table row should appear http://jsfiddle.net/shalman44/92drs7ax/13/ – imran_44 Oct 25 '21 at 11:12
0

I just searched google with "bootstrap multiselect" and i found this codepen

    $(document).ready(function() {
        $('#multiple-checkboxes').multiselect({
          includeSelectAllOption: true,
        });
    });
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css">


<div class="">
    <strong>Select Language:</strong>
    <select id="multiple-checkboxes" multiple="multiple">
        <option value="php">PHP</option>
        <option value="javascript">JavaScript</option>
        <option value="java">Java</option>
        <option value="sql">SQL</option>
        <option value="jquery">Jquery</option>
        <option value=".net">.Net</option>
    </select>
</div>
Gilhyeon
  • 59
  • 1
  • 4