Select2: OPtion ajax is not allowed for Select2 when attached to a <select> element

Created on 3 Jan 2013  路  11Comments  路  Source: select2/select2

I am receiving the error above with Select2 3.2 while calling select2() with the 'ajax' option
specified. I found another issues related to Select2 with a multi select widget but this is not the case here. I have multiple select elements with a pretty long option list and now I am trying to move the code to ajax.

Most helpful comment

@zopyx LOL go fuck yourself.

All 11 comments

this is a documented limitation. if you want to use ajax you have to attach to a hidden input tag.

Text field not working with select2.

@ivaynberg This does not appear to be documented anymore, at least not on the ajax examples section of the page, there is also isn't any HTML shown for the ajax example either.

I think no need to go with hidden input element. You can give a try, get plain html data from ajax call and set it in <select> and then init select2 resetting method. Here's code snippet
HTML

<select id="select" name="select" class="select2">
        <option value="" selected disabled>Please Select Above Field</option>
</select>

Javascript

        $.ajax({
            type: "POST",
            cache:false,
            url: YOUR_AJAX_URL,
            success: function(response)
            {
                $('#select').html(response);
            }
        });
        $('#select').select2("val","");

Ajax Response :

<option value="value">Option Name</option>
.
.
.
<option value="value">Option Name</option>

@ivaynberg wonder if you can explainthe reason for using a hidden field for multuple + ajax?
Genuine question.

@ivaynberg wonder if you can explainthe reason for using a hidden field for multuple + ajax?
Genuine question.

@kyle-sorensen @dnagir There is more information about this issue, and how it will be fixed in Select2 4.0 in https://github.com/ivaynberg/select2/issues/279.

I met this problems too, I want to know why

What a stupid "me too, me too"...

@zopyx HaHa, I have give up that way

@zopyx LOL go fuck yourself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ethanclevenger91 picture ethanclevenger91  路  3Comments

JuanWilde picture JuanWilde  路  3Comments

efusionsoft picture efusionsoft  路  3Comments

hipertracker picture hipertracker  路  3Comments

actricom picture actricom  路  3Comments