Need Select data with all options checkbox using Select2 JS. I tried to select all options in Select2 JS using checkbox and default select All Options.How to Set default All options Checked in Select2 JS.
Script code:
<script type="text/javascript">
$(".js-select2").select2({
closeOnSelect : false,
placeholder : "Placeholder",
allowHtml: true,
allowClear: true,
tags: true // создает новые опции на лету
});
</script>
Html Code:
<select class="js-select2" multiple="multiple">
<option value="O1" data-badge="">Option1</option>
<option value="O2" data-badge="">Option2</option>
<option value="O3" data-badge="">Option3</option>
<option value="O4" data-badge="">Option4</option>
<option value="O5" data-badge="">Option5</option>
<option value="O6" data-badge="">Option6</option>
<option value="O7" data-badge="">Option7</option>
<option value="O8" data-badge="">Option8</option>
<option value="O9" data-badge="">Option9</option>
<option value="O10" data-badge="">Option10</option>
<option value="O11" data-badge="">Option11</option>
<option value="O12" data-badge="">Option12</option>
<option value="O13" data-badge="">Option13</option>
</select>
Need to select like,
You will need to create the Checkbox yourself next to select box.
<input type="checkbox" id="checkbox" >Select All
Then you will need to bind the click event of this checkbox to select all the options of select2 dropdown.
$(document).ready(function() {
$(".js-select2").select2({
closeOnSelect : false,
placeholder : "Placeholder",
allowHtml: true,
allowClear: true,
tags: true // создает новые опции на лету
});
$("#checkbox").click(function(){ //Bind the Click event of Checkbox
if($('#checkbox').is(':checked') ){ //If checkbox is checked. Select All Items.
$(".js-select2 > option").prop("selected","selected");
$(".js-select2").trigger("change");
}else{ //Otherwise Deselect all items.
$(".js-select2").val(null).trigger('change');
}
});
$("#checkbox").click(); //Manually click the checkbox so that it is selected 1st time.
});
You can check the Demo Here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.