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);
});
Info: The values for gSiteIDs are the values used when originally dynamically creating the select options. #selAllSites is the id of a checkbox and #siteID is the id of the select list you want to manipulate.
var gSiteIDs = "8475, 9082, 2387, 1234";
function selectAllSites()
{
if($("#selAllSites").is(":Checked")) {
$("#siteID").select2("val", [gSiteIDs]);
} else {
$("#siteID").select2("val", []);
}
}
using Select 2 DEMO
$("#e1").select2();
$("#checkbox").click(function(){
if($("#checkbox").is(':checked') ){
$("#e1 > option").prop("selected","selected");// Select All Options
$("#e1").trigger("change");// Trigger change to select 2
}else{
$("#e1 > option").removeAttr("selected");
$("#e1").trigger("change");// Trigger change to select 2
}
});
$("#button").click(function(){
alert($("#e1").val());
});
<select multiple id="e1" style="width:300px">
<option value="AL">Alabama</option>
<option value="Am">Amalapuram</option>
<option value="An">Anakapalli</option>
<option value="Ak">Akkayapalem</option>
<option value="WY">Wyoming</option>
</select>
<input type="checkbox" id="checkbox" >Select All
<input type="button" id="button" value="check Selected">
You need code As shown in DEMO2 for Simple Select
$("#checkbox").click(function(){
if($("#checkbox").is(':checked') ){
$("select > option").prop("selected","selected");
}else{
$("select > option").removeAttr("selected");
}
});
$("#button").click(function(){
alert($("select").val());
});
<select multiple size=2>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select> <input type="checkbox" id="checkbox" >Select All
<input type="button" id="button" value="check Selected">
$(document).ready(function() {
$("#my-select").select2();
});
function selectAll() {
$("#my-select > option").prop("selected", true);
$("#my-select").trigger("change");
}
function deselectAll() {
$("#my-select > option").prop("selected", false);
$("#my-select").trigger("change");
}
.button-container {
margin-bottom: 10px;
}
#my-select {
width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet"/>
<div class="button-container">
<button type="button" onclick="selectAll()">Select All</button>
<button type="button" onclick="deselectAll()">Deselect All</button>
</div>
<select id="my-select" multiple>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grape">Grape</option>
<option value="pineapple">Pineapple</option>
</select>
This is the simplest way
function selectAll() {
$("#my-select > option").prop("selected", true);
$("#my-select").trigger("change");
}
Custom implementation of Select all in select2 DEMO
function RunSelect2(){
$('#select-id').select2({
allowClear: true,
closeOnSelect: false,
}).on('select2:open', function() {
setTimeout(function() {
$(".select2-results__option .select2-results__group").bind( "click", selectAlllickHandler );
}, 0);
});
}
RunSelect2();
var selectAlllickHandler = function() {
$(".select2-results__option .select2-results__group").unbind( "click", selectAlllickHandler );
$('#select-id').select2('destroy').find('option').prop('selected', 'selected').end();
RunSelect2();
};
.select2-results__group
{
cursor:pointer !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet"/>
<select multiple id='select-id' style="width:300px">
<optgroup label="select all">
<option value='1'>1 one</option>
<option value='2'>2 two</option>
<option value='3'>3 three</option>
<option value='4'>4 four</option>
<option value='5'>5 five</option>
<option value='6'>6 six</option>
</optgroup>
</select>
If you need a checkbox for this action, just do this simply.
<select id="myselect" multiple="multiple">
<option value="level1">Level 1</option>
<option value="level2">Level 2</option>
<option value="level3">Level 3</option>
<option value="level4">Level 4</option>
</select>
<br/>
Select/Deselect <input type="checkbox" id="selectall"/>
Jquery function:
$("#selectall").on('click',function(){
var checked = $("#selectall").prop("checked");
if(checked==true){
$("#myselect > option").prop("selected",true);
}else{
$("#myselect > option").prop("selected",false);
}
});