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
Please see below code.
$('.select2').select2({
formatResult:function(object, container, query){
if(object.id=='all' || object.id=='clear')
return ' '+object.text+'';
return object.text;
}
});
$('.select2').on("change", function(e) {
if($.inArray('all', e.val)===0){
var selected = [];
$(this).find("option").each(function(i,e){
if($(e).attr("value")=='all' || $(e).attr("value")=='clear')
return true;
selected[selected.length]=$(e).attr("value");
});
$(this).select2('val',selected);
}else if($.inArray('clear', e.val)===0){
$(this).select2('val','');
}
});
Reference from: https://github.com/select2/select2/issues/195#issuecomment-52163095