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
$(document).ready(function() {
$("#checkbox").click(function(){
if($("#checkbox").is(':checked') ){ //select all
$("#e1").find('option').prop("selected",true);
$("#e1").trigger('change');
} else { //deselect all
$("#e1").find('option').prop("selected",false);
$("#e1").trigger('change');
}
});
});
Select All
If options are created after AJAX request then these options don't work. So we map them with find and select/unselect them , after that trigger change.
HTML
Select All
JS
$(document).ready(function() {
$("#checkbox").click(function(){
if($("#checkbox").is(':checked') ){ //select all
$("#e1").find('option').prop("selected",true);
$("#e1").trigger('change');
} else { //deselect all
$("#e1").find('option').prop("selected",false);
$("#e1").trigger('change');
}
});
});
just use find.