6

I am using the Bootstrap-Multiselect (http://davidstutz.github.io/bootstrap-multiselect/)

I have two dropdowns, when an item is selected in one dropdown I need to reset the other dropdown. I have been attempting to do this using the onchange jQuery but without success.

If an option is selected from dropdown1, then deselect/reset everything in dropdown2.

<form>
    <select id="dropdown1" name="dropdown1" multiple="multiple">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>

If an option is selected from dropdown2, then deselect/reset everything in dropdown1.

    <select id="dropdown2" name="dropdown2" multiple="multiple">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
</form>

My failed jQuery

$("#dropdown1").change(function () {
    $('#dropdown2').multiselect('refresh');
});

$("#dropdown2").change(function () {
    $('#dropdown1').multiselect('refresh');
});

How should this be accomplished?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Terry
  • 1,621
  • 4
  • 25
  • 45

3 Answers3

12

I suspect you need to reset the select element and then do the refresh, like so:

$("#dropdown1").change(function () {
    $('#dropdown2 option:selected').each(function() {
        $(this).prop('selected', false);
    })
    $('#dropdown2').multiselect('refresh');
});

$("#dropdown2").change(function () {
    $('#dropdown1 option:selected').each(function() {
        $(this).prop('selected', false);
    })
    $('#dropdown1').multiselect('refresh');
});

That code is borrowed from the reset button code on the Bootstrap Multiselect site. See the further examples

d3v_1
  • 579
  • 3
  • 12
9

Another way of deselecting all options. (Because multiselect('refresh') did not work for me.)

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

This is working for me.

$('#ID').val('').multiselect('refresh');
UWU_SANDUN
  • 1,123
  • 13
  • 19