All answers that include an empty option at first position of select element is correct, but only work when there's no option selected.
I used @Colin's solution - $('.wcl-select').prepend('<option selected=""></option>').select2({placeholder: "Select Month"})
- however when we've one option selected, we will have two options marked with selected, not correctly displaying the selected item:
I will check, before applying Select2, if there's any option selected, then create empty option in the first position:
$(".wcl-select").each(function(_, element) {
if (!$(element).find('option[selected]').length) {
$(element).prepend("<option value='' selected='selected'></option>");
}
});
$(".wcl-select").select2({
theme: "bootstrap-5",
width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style',
placeholder: $(this).data('placeholder'),
allowClear: false,
closeOnSelect: true,
multiple: false,
});