6

I'm using bootstrap-multiselect for a dropdown in my application. Everything works fine except for my form reset, when I want it to revert back to the originally selected values. It's displaying the selected values as expected in the menu when it's closed (i.e. creating a list with multiple selections), but when I open the menu the checkboxes for the selected items aren't checked.

I've tried the following to no avail:

$("#MyMenu").multiselect('refresh');
$("#MyMenu").multiselect('rebuild');
$("#MyMenu").multiselect('destroy');

followed by

$("#MyMenu").multiselect();

Any help is appreciated!

jenni-mm2
  • 185
  • 1
  • 2
  • 11

8 Answers8

9

For me it works simply with:

$('#id').multiselect('refresh');

However, it must be noted that the reset event fires before the form inputs are actually reset. Practically speaking, this means that the following won't work:

$('#formID').on('reset', function(){
    $('#id').multiselect('refresh');
});

while wrapping the call inside of an instantaneous setTimeout will work!

$('#formID').on('reset', function(){
    setTimeout(function(){
        $('#id').multiselect('refresh');
    });
});
Dre
  • 1,985
  • 16
  • 13
8

today i faced same issue...i did

$("option:selected").removeAttr("selected");
$("#MyMenu").multiselect('refresh');

It worked for me...

Bhupendra Pandey
  • 348
  • 3
  • 16
2

After much trial and error, I finally resolved it like this.

$('#MyMenu').multiselect('destroy');
$('#MyMenu').multiselect();

$('#MyMenu option:selected').each(function () {
    $(":checkbox[value=" + $(this).val() + "]").attr('checked', true)
})
jenni-mm2
  • 185
  • 1
  • 2
  • 11
  • Based on your answer I did this, and it seems to work: `$('option:selected', theMultiselect$).each(function() { theMultiselect$.multiselect('deselect', $(this).val()); });` – Mikael Oct 01 '14 at 23:46
  • I tried that in the JSFiddle above and it doesn't seem to work. – jenni-mm2 Oct 03 '14 at 13:20
0

str is array from database

$('#dropdown').multiselect({includeSelectAllOption: true,});
$("#dropdown").multiselect('deselectAll', false);

initialize and reset the doropdown first.

for(i=0; i<= str.length; i++){           
      $("#dropdown").find("option[value='"+ $.trim(str[i])+"']").prop("selected", true);
      $("#dropdown").find("option[value='"+ $.trim(str[i])+"']").attr("selected", "selected");          
}

$("#dropdown").multiselect("refresh");

this code will preselect the values.

Also, if you want to reset the dropdown below is the code

$('#dropdown').multiselect({includeSelectAllOption: true,});
$("#dropdown").multiselect('deselectAll', false);

$("#dropdown").multiselect("refresh");

Do not forget to refresh. this code is tried and tested. works for me.

Jbryson
  • 2,875
  • 1
  • 31
  • 53
Dino
  • 1
  • 1
0

To reset the multiselect to its original state at page load, try:

// Initialize Multiselect
var msDropdownInitialSelected;
$(document).ready(function () {
    $('#msDropdown').multiselect();
    $('#msDropdown').multiselect('select', valuesArray);

    msDropdownInitialSelected = $('#msDropdown').val();
});

// Reset Multiselect
$('msDropdown').on('reset', function(){
    $('#msDropdown').multiselect('select', msDropdownInitialSelected);
    $('#msDropdown').multiselect('refresh');
});
OneManBand
  • 528
  • 5
  • 24
0

Resetting multiselect worked for me by executing below code in sequence.

$('#ID').multiselect('deselectAll', false);    
$('#ID').multiselect('select', []);   
Marc Dix
  • 1,864
  • 15
  • 29
Arun
  • 1
0

None of above but following worked for me.

$('#ID').multiselect('deselectAll', false);    
$('#ID').multiselect('updateButtonText');
Pooja Mistry
  • 6,835
  • 1
  • 13
  • 8
-1

This is the code that worked for me, all others could not work.

$('#basicForm').on('reset', function(){ 
    // clearing the categories
    $('#project_category').select2('val', null);
    $('#project_category').attr('value', '');
});
FelixSFD
  • 6,052
  • 10
  • 43
  • 117