select2 v4 - Programmatic set value with ajax option

16,848 views
Skip to first unread message

marcinmar...@gmail.com

unread,
Apr 10, 2015, 5:41:44 AM4/10/15
to sel...@googlegroups.com
How I can set value when I use ajax option in select ?

pcco...@gmail.com

unread,
May 17, 2015, 8:09:58 AM5/17/15
to sel...@googlegroups.com, marcinmar...@gmail.com
On Friday, April 10, 2015 at 5:41:44 AM UTC-4, marcinmar...@gmail.com wrote:
> How I can set value when I use ajax option in select ?

I am looking for this answer also. Have a completely ajax application and trying to trigger an ajax lookup when setting the value of a select2 field. I don't think it can be done. :(

Paul Colella

unread,
May 18, 2015, 7:10:26 PM5/18/15
to sel...@googlegroups.com, marcinmar...@gmail.com
On Friday, April 10, 2015 at 5:41:44 AM UTC-4, marcinmar...@gmail.com wrote:
> How I can set value when I use ajax option in select ?

This is the answer I have come up with until I find a better solution.

Do an ajax request to get a select2 data object (id,text, etc..) of the selected value.

Then:

var s2 = $("#selectelement").data('select2');
s2.trigger('select', {
data: objectfromajax
});

This will add the option to your select element and make it selected.

Kevin Brown

unread,
May 19, 2015, 1:37:55 PM5/19/15
to sel...@googlegroups.com, marcinmar...@gmail.com
This recently came up on Stack Overflow a few times:


If you are using a basic option (id and text only), you can just append the `<option>` to the `<select>` (like you would do without Select2).

    <option value="id" selected="selected">text</option>

And afterwards you would have to trigger the `change` event, so Select2 can react to the new selection.

Using the internal event system to trigger a `select` event also works, but I'd consider it to be undocumented for now. But since you mentioned it, you should be able to just do

    $('#selectelement').select2('trigger', 'query', {
      data: dataObject
    });

Which works for any method on the main Select2 object.

Kevin



--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.

Paul Colella

unread,
May 19, 2015, 3:53:05 PM5/19/15
to sel...@googlegroups.com, marcinmar...@gmail.com, ke...@kevin-brown.com
In my case I'm using addition properties returned by the query, setting other fields on the form with those results, etc, etc... so appending options to the select is not a solution.

Thanks for the trigger query hint. I'll play with that also.

Paul

sume...@gmail.com

unread,
Jun 22, 2015, 10:41:29 AM6/22/15
to sel...@googlegroups.com, marcinmar...@gmail.com
Dear Paul,
Thanks a lot. I was facing the same issue and as you said simply adding <option> tag wont help as I am using other information (not 'id' and 'text')

Thanks a lot
Sumesh

Kevin Brown

unread,
Jun 22, 2015, 1:52:25 PM6/22/15
to sel...@googlegroups.com, marcinmar...@gmail.com
Some of you may be interested in the discussion around adding additional attributed to `<option>` tags, for this exact use case.


Feel free to weigh in on the options, or discuss other ways of doing what you are looking for.

sume...@gmail.com

unread,
Jul 3, 2015, 11:22:36 AM7/3/15
to sel...@googlegroups.com, marcinmar...@gmail.com
On Friday, April 10, 2015 at 3:11:44 PM UTC+5:30, marcinmar...@gmail.com wrote:
> How I can set value when I use ajax option in select ?

Hi,

I was trying to pragmatically set values in select2(v4) having multiple selection and Ajax
I tried the solution mentioned by Paul, but didn't worked.
But it worked in the case of single-selection select2
This is how I set the data. Please help

var s2 = $("#selectelement").data('select2');
s2.trigger('select', {

data: [{"id":"15","text":"T1"},{"id":"28","text":"T2"}]
});

devm0...@gmail.com

unread,
Sep 23, 2016, 12:16:23 PM9/23/16
to select2, marcinmar...@gmail.com, ke...@kevin-brown.com, Me
Hi Kevin,
Are you able to provide the select2 v4 equivalent of this approach? I have an ajax control that I only have the 'id', and need to make sure the full data with 'text' is pulled from the remote datasource.

Thanks!
Mike



On Tuesday, May 19, 2015 at 1:37:55 PM UTC-4, Kevin Brown wrote:

4e....@gmail.com

unread,
Jan 20, 2017, 8:41:42 AM1/20/17
to select2, marcinmar...@gmail.com
пятница, 10 апреля 2015 г., 12:41:44 UTC+3 пользователь marcinmar...@gmail.com написал:
> How I can set value when I use ajax option in select ?

Hi all this how I worked it out:

$("#selectelement").select2("trigger", "select", {
data: { id: "1",text:"name" }
});

ayush...@kaizeninfotech.co.in

unread,
Mar 9, 2017, 5:09:18 AM3/9/17
to select2, marcinmar...@gmail.com
On Friday, April 10, 2015 at 3:11:44 PM UTC+5:30, marcinmar...@gmail.com wrote:
> How I can set value when I use ajax option in select ?

Its not working.

ayush...@kaizeninfotech.co.in

unread,
Mar 9, 2017, 5:09:44 AM3/9/17
to select2, marcinmar...@gmail.com, 4e....@gmail.com
Not working

vlad.a...@gmail.com

unread,
Mar 15, 2017, 5:26:23 AM3/15/17
to select2, marcinmar...@gmail.com
> How I can set value when I use ajax option in select ?

var control = $('#selectControl').data('select2');
var adapter = control.dataAdapter;
adapter.addOptions(adapter.convertToOptions(response));
$('#selectControl').trigger('change');

saabir...@gmail.com

unread,
Feb 15, 2018, 4:29:47 PM2/15/18
to select2
On Friday, 10 April 2015 12:41:44 UTC+3, marcinmar...@gmail.com wrote:
> How I can set value when I use ajax option in select ?

// 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 = new Option(data.full_name, data.id, true, true);
studentSelect.append(option).trigger('change');

// manually trigger the `select2:select` event
studentSelect.trigger({
type: 'select2:select',
params: {
data: data
}
});
});

Reply all
Reply to author
Forward
0 new messages