I\'m using jQuery select2 multi select dropdown. I need to select all options in a dropdown from code. Basically there is a Select All checkbox on which this functionality h
This line of code will select all options
$('select.select-all-class-name').attr('selected', true).parent().trigger('change');
Add select events to work for all scenarios. The above line fails in a scenario, when you select and deselect an option and then click on selectAll. The last deselected option will not get selected. In order to fix that Add the below line of code.
//Select options selected true for the selected option
$('#mySelect2').on('select2:selecting', function (e) {
$('select#mySelect2ID > option[value="'+e.params.args.data.id+'"]').attr('selected', true);
});
//DeSelect options selected to false for the option deselected
$('#mySelect2').on('select2:unselecting', function (e) {
$('select#mySelect2ID > option[value="'+e.params.args.data.id+'"]').attr('selected', false);
});