Bootstrap-Multiselect issue with form reset and checkboxes repopulating
Asked Answered
B

8

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!

Bugs answered 23/9, 2014 at 19:52 Comment(4)
Is that the exact syntax you used? Please don't leave us to assume what else was in your code. I'm guessing you actually tried $("#MyMenu").multiselect('refresh');, for example.Irritation
Sorry, I updated the code to be more accurate.Bugs
Hmmm... That shoulda done it. Maybe create a demo. jsfiddle.netIrritation
Here it is: JSFiddleBugs
L
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');
    });
});
Lallans answered 13/11, 2014 at 15:33 Comment(0)
C
8

today i faced same issue...i did

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

It worked for me...

Carmel answered 22/12, 2014 at 9:42 Comment(0)
B
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)
})
Bugs answered 30/9, 2014 at 16:15 Comment(2)
Based on your answer I did this, and it seems to work: $('option:selected', theMultiselect$).each(function() { theMultiselect$.multiselect('deselect', $(this).val()); });Plank
I tried that in the JSFiddle above and it doesn't seem to work.Bugs
P
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.

Predate answered 9/4, 2015 at 13:58 Comment(1)
Can you format your code properly? It's unreadable at the moment.Kalmia
C
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');
});
Cargile answered 13/1, 2016 at 16:50 Comment(0)
O
0

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

$('#ID').multiselect('deselectAll', false);    
$('#ID').multiselect('select', []);   
Oubre answered 1/6, 2016 at 10:53 Comment(0)
N
0

None of above but following worked for me.

$('#ID').multiselect('deselectAll', false);    
$('#ID').multiselect('updateButtonText');
Nephro answered 28/6, 2017 at 13:52 Comment(0)
D
-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', '');
});
Deerstalker answered 13/2, 2017 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.