jqueryjquery-uijquery-ui-multiselect

How to select and deselect group of options in jquery multiselect


I know I can pass 'checkAll' and 'uncheckAll' to my multiselect instance, but how do I deselect a group of options?

var deselectOptions = [1, 2, 3];

$('#my_select').multiselect('checkAll'); //except deselect options

Solution

  • Try this for checking all except:

    var deselectOptions = [1, 2, 3];
    
    $('#my_select').multiselect('checkAll');
    
    $('#my_select :selected').each(function () {
        if (deselectOptions.includes(parseInt($(this).val()))) {
            $(this).prop('selected', false);
        }
    });
    
    $('#my_select').multiselect('refresh');
    

    or the opposite:

    var selectOptions = [1, 2, 3];
    
    //may not be required depending on use case
    $('#my_select').multiselect('uncheckAll');
    
    $('#my_select option').each(function () {
        if (selectOptions.includes(parseInt($(this).val()))) {
            $(this).prop('selected', true);
        }
    });
    
    $('#my_select').multiselect('refresh');