Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 multi select blank option always selected

I am using select2 for multi select and when I select first time , any option , null value from blank option always get selected. And another problem if I don't use placeholder first option get selected when loaded.

Here is my code for html(it includes some php):

<select class="form-control select2-multiple" name="category_id">
        <option></option>
        @foreach($instrument_category as $key=>$value)
            <option value="{{ $value }}">{{ $value }}</option>
        @endforeach
</select>

here is my jquery:

$(".select2-multiple").select2({
    'placeholder' : "Select Category",
    'multiple' : true,
    'defaultView': 'dropdown'
});

Again problem : blank value get selected when selected for first time.

Screenshot given.

blank value selected

like image 738
vishwas jadav Avatar asked Apr 20 '18 13:04

vishwas jadav


People also ask

How do I stop Select2 from auto selecting the first option?

Your best option is to use the placeholder support in Select2 and include a blank option tag as your default selection.

How to placeholder in Select2?

js-example-placeholder-single"). select2({ placeholder: "Select a state", allowClear: true }); For single selects only, in order for the placeholder value to appear, you must have a blank <option> as the first option in your <select> control. This is because the browser tries to select the first option by default.

How to add placeholder in Select2 js?

Select2 uses the native placeholder attribute on input boxes for the multiple select, and that attribute is not supported in older versions of Internet Explorer. You need to include Placeholders. js on your page, or use the full build, in order to add placeholder attribute support to input boxes.

How do I set Select2 values?

// Set up the Select2 control $('#mySelect2').select2({ ajax: { url: '/api/students' } }); // Fetch the preselected item, and add to the control var studentSelect = $('#mySelect2'); $.ajax({ type: 'GET', url: '/api/students/s/' + studentId }).then(function (data) { // create the option and append to Select2 var option ...


2 Answers

As it's stated in Select2 Documentation https://select2.org/placeholders :

For multi-selects, you must not have an empty element

This leads to the problem that the browser selects the first option of "select" by default, so as a workaround there is a hacky way that worked for me

Either use this :

$your_select_element.select2({
    multiple: true,
    placeholder: "Select whatever..."
});
// set the value to null or empty string '', then trigger (change) event
$your_select_element.val(null).trigger("change");

or this :

// the initial call of select2
$your_select_element.select2({
    multiple: true,
    placeholder: "Select whatever..."
});
$your_select_element.val(null);
$('.select2.select2-container').remove();
//re-call select2 on your element 
$your_select_element.select2({
    multiple: true,
    placeholder: "Select whatever..."
});

It's no-brainer that the first way is the way to go ;) Hope this helps someone

like image 185
medBouzid Avatar answered Oct 23 '22 13:10

medBouzid


I had the exact same issue. To resolve it: - add the multiple attribute to your select (multiple="multiple") - remove the empty option tag

In my case, I create the dropdowns dynamically. So I wrote this code to fix it.

            $('#' + selectorId).attr('multiple', 'multiple');
            $("#" + selectorId + " option")[0].remove();

            $("#" + selectorId).select2({
                multiple: true,
                width: '100%',
                placeholder: "-- Select --"
            });
like image 30
Javaman1138 Avatar answered Oct 23 '22 13:10

Javaman1138