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 works great with AJAX, prevents from opening Bookmarks window in FF (Ctrl-D), and works fine when closeOnSelect is set to off.
$(document).on("keypress",".select2-input",function(event){
if (event.ctrlKey || event.metaKey) {
var id =$(this).parents("div[class*='select2-container']").attr("id").replace("s2id_","");
var element =$("#"+id);
if (event.which == 97){
var selected = [];
$('.select2-drop-active').find("ul.select2-results li").each(function(i,e){
selected.push($(e).data("select2-data"));
});
element.select2("data", selected);
element.select2("focus");
event.preventDefault();
} else if (event.which == 100){
element.select2("data", []);
event.preventDefault();
}
}
});