bootstrap multiselect - De-select / Uncheck all options in a single click
Asked Answered
A

10

10

I'm using Bootstrap multiselect and I would like to add an option to uncheck all selected options. Please provide me with a solution for the same. Thanks for any help :)

Anacardiaceous answered 27/3, 2014 at 11:38 Comment(2)
possible duplicate of Quick way to clear all selections on a multiselect enabled <select> with jQuery?Retroactive
@Retroactive This is a bootstrap multiselect, it's different.Olecranon
S
-5

Try this

$("option:selected").prop("selected", false)
Stercoraceous answered 27/3, 2014 at 11:41 Comment(1)
This doesn't work for me too, What worked for me is $('#multi-select-dropdown').multiselect("deselectAll", false).multiselect("refresh");Euniceeunuch
M
20

You can use .multiselect('refresh') method as mentioned in the bootstrap-multiselect documentation here.

So, add an HTML button / icon next to the multi-select select list and then use the below code:

$("#reset_client").click(function(){
  $('option', $('#select_client')).each(function(element) {
    $(this).removeAttr('selected').prop('selected', false);
  });
  $("#select_client").multiselect('refresh');
});
<select name="clients[]" multiple="multiple" id="select_client" style="display: none;">
    <option value="multiselect-all"> Select all</option>
    <option value="1">Client 1</option>
    <option value="2">Client 2</option>
    <option value="3">Client 3</option>
    <option selected="selected" value="4">Client 4</option>
    <option selected="selected" value="5">Client 5</option>
    <option value="6">Client 6</option>
    <option selected="selected" value="7">Client 7</option>
    <option value="8">Client 8</option>
</select>

<input type="button" value="Reset" name="reset_clients" id="reset_client" class="reset_button" title="Clear Selection">
Mainmast answered 17/9, 2015 at 17:17 Comment(1)
Working for me too. Thanks.Hatchway
G
11

Use below code to deselect all select item in multiselect bootstrap dropdown

$("#ddlMultiSelectId").multiselect('clearSelection');
Goodsell answered 5/12, 2018 at 9:47 Comment(0)
O
9

You can include a "select all" button, pushing that twice would "unselect all". To do this add this option:

includeSelectAllOption:true

This will deslect all for a bootstrap multiselect based on a select element.

function multiselect_deselectAll($el) {
    $('option', $el).each(function(element) {
        $el.multiselect('deselect', $(this).val());
    });
}

$('.multiselect').each(function() {
    var select = $(this);
    multiselect_deselectAll(select);
});

You could add an option by hand that says "Select all" and then when it's pressed call the reset function.

See: https://github.com/davidstutz/bootstrap-multiselect/issues/271

Olecranon answered 30/5, 2015 at 15:43 Comment(0)
V
5

You can do this with the "deselectAll" method.

$("select.multiselect").multiselect("deselectAll", false);

It is important that you tell jQuery to target your "select's" only, otherwise it will fail. This is because the plugin adds something else with the same class name.

@L_7337 - This is not a duplicate of the post you link to. This post is about how to deselect all options, if you use Bootstrap Multiselect

The other post, is how to deselect, from a normal multiselect option.

Best regards Rasmus

Venue answered 27/3, 2015 at 11:26 Comment(0)
H
2

If you have the below multi-select:

<div class="form-group">
      <label>Choose Skills</label>
      <select id="DDL" name="selValue" class="selectpicker form-control"  multiple>
               <option value="1">One</option>
               <option value="2">Two</option>
               <option value="3">Three</option>
        </select>
  </div>

Then you can use the below jQuery to deselect all option on a click of a button:

$('#DDL').selectpicker("deselectAll", true).selectpicker("refresh");
Hooked answered 21/1, 2018 at 22:19 Comment(0)
D
1

Try this:

$("#select_id option:selected").removeAttr("selected");
Dragone answered 18/6, 2015 at 4:50 Comment(1)
@dhh, the user explains the scenario already, and there is already a selected answer which does not work. The answer I posted is just to correct the selected answer. The user has not given any code scenario that I can help him on how to make this work. Also, we are not here to do homework for people right? We just suggest the way/path to reach your goal. Thanks anyway for your precious comment.Dragone
S
0

The issue was fixed in a gitlab fork here

it says:

Fixed trigger onSelectAll when deselecting Select All

Alternatively we can wait while issue #752 will be resolved. It was raised only one month ago.

Scoles answered 30/6, 2016 at 1:29 Comment(0)
A
0
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class = "container">
<div class="modal show" id="myModal" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button class="close" type="button" data-dismiss="modal">×</button>
        <nav class="navbar navbar-default">
  <div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Channel(s)</a></li>
      <li><a href="#">Collaborator(s)</a></li>
    </ul>
  </div>
</nav>
      </div>
      <div class="modal-body">
        <form>              <div class="form-check row">
            <label class="col-sm-2 form-check-label" for="ListeningProject">Listening Project</label>
              <div class="col-sm-10">
                <input class="form-check-input" id="ListeningProject" value="" type="checkbox" />
              </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 form-check-label" for="AccountName">Select:</label>
            <div class="col-sm-10">
                <select class="form-control" id="AccountName" multiple="" required="">
                  <option>A Company</option>
                  <option>B Copany</option>
                  <option>C Company</option>
                  <option>D Company</option>
                </select>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
</div>
<script>
$("#ListeningProject").change(function(){  //"select all" change
    var status = this.checked; // "select all" checked status
    $('#AccountName option').prop('selected', status);
});
</script>
</body>
</html>
Aflame answered 22/10, 2018 at 7:56 Comment(0)
E
0

You can do like this, it works for me:

$('#dropdownID').val([]).multiselect('refresh')
Encyclopedia answered 13/6, 2019 at 8:4 Comment(0)
S
-5

Try this

$("option:selected").prop("selected", false)
Stercoraceous answered 27/3, 2014 at 11:41 Comment(1)
This doesn't work for me too, What worked for me is $('#multi-select-dropdown').multiselect("deselectAll", false).multiselect("refresh");Euniceeunuch

© 2022 - 2024 — McMap. All rights reserved.