How to get all the values in a Select2 dropdown?
Asked Answered
W

3

9

How can we get all the elements that are in the jQuery Select2 dropdown plugin.

I have applied Select2 to a input type = hidden, and then populated it using Ajax.

Now at one instance I need to get all the values that are appearing in the dropdown.

Here is a input field.

<input type="hidden" id="individualsfront" name="individualsfront" style="width:240px" value="" data-spy="scroll" required />

and to this input field I have applied this

$("#individualsfront").select2({
    multiple: true,
    query: function (query){
        var data = {results: []};
        $.each(yuyu, function(){
            if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){
                data.results.push({id: this.id, text: this.text });
            }
        });
        query.callback(data);
    }
});

The yuyu is a json coming from some AJAX call and populating the Select2.

Now I want in some other code a way to get all the values inside the Select2.

Westmorland answered 24/4, 2013 at 8:57 Comment(2)
are you talking about this??Lucilucia
Yes i am talking about the Select2 plugin. That you have mentioned in the link.Westmorland
H
5

option 1: you can use directly the data object

var results = [];
$("#individualsfront").select2({
    multiple: true,
    query: function (query){
        var data = {results: []};
        $.each(yuyu, function(){
            if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){
                data.results.push({id: this.id, text: this.text });
            }
        });

        results = data.results; //<=====  
        query.callback(data);
    }
});

and use it like this for example:

$('#individualsfront').on('open',function(){
    $.each(results, function(key,value){
        console.log("text:"+value.text);
    });
});

option 2: request the feature and use (temporary) something like:

$('#individualsfront').on('open',function(){
    $(".select2-result-label").each(function()
        {
            console.log($(this).text());
        })
    });
});

http://jsfiddle.net/ouadie/qfchH/1/

option 3: you can use .select2("data"); but it returns all elements only if there is no selected element.

var arrObj = $("#individualsfront").select2("data");
for each(yuyu in arrObj)
{
   console.log("id: "+yuyu.id+ ", value: "+yuyu.text);
}

Hysterics answered 24/4, 2013 at 9:23 Comment(7)
$("#yourselect").select2("data");Hysterics
Ouadie it just gives me the value of the selected item, not all the elements of the select2.Westmorland
@Hysterics your option3 returns null if there is no selected elementPrecontract
@Sam-Selikoff The last time I tried it it returns all elements, I'll remove this optionHysterics
Is there any other way to get it, just by selecting the element (i.e. without responding to an event)?Precontract
I don't think, here is the documentation ivaynberg.github.io/select2/#documentationHysterics
Cannot we do something like this $("#individualsfront").data('select2').opts.data. ?Reactance
D
6

I was trying to figure this out myself and found that you can get at least the options in a select2.

var options = $("#dropdown").find("option")
var optionsText = $("#dropdown").find("option[value='1']").text()
Departed answered 9/3, 2015 at 22:13 Comment(0)
H
5

option 1: you can use directly the data object

var results = [];
$("#individualsfront").select2({
    multiple: true,
    query: function (query){
        var data = {results: []};
        $.each(yuyu, function(){
            if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){
                data.results.push({id: this.id, text: this.text });
            }
        });

        results = data.results; //<=====  
        query.callback(data);
    }
});

and use it like this for example:

$('#individualsfront').on('open',function(){
    $.each(results, function(key,value){
        console.log("text:"+value.text);
    });
});

option 2: request the feature and use (temporary) something like:

$('#individualsfront').on('open',function(){
    $(".select2-result-label").each(function()
        {
            console.log($(this).text());
        })
    });
});

http://jsfiddle.net/ouadie/qfchH/1/

option 3: you can use .select2("data"); but it returns all elements only if there is no selected element.

var arrObj = $("#individualsfront").select2("data");
for each(yuyu in arrObj)
{
   console.log("id: "+yuyu.id+ ", value: "+yuyu.text);
}

Hysterics answered 24/4, 2013 at 9:23 Comment(7)
$("#yourselect").select2("data");Hysterics
Ouadie it just gives me the value of the selected item, not all the elements of the select2.Westmorland
@Hysterics your option3 returns null if there is no selected elementPrecontract
@Sam-Selikoff The last time I tried it it returns all elements, I'll remove this optionHysterics
Is there any other way to get it, just by selecting the element (i.e. without responding to an event)?Precontract
I don't think, here is the documentation ivaynberg.github.io/select2/#documentationHysterics
Cannot we do something like this $("#individualsfront").data('select2').opts.data. ?Reactance
A
3
function select2Options($elementSelect2) {
    var data = [],
        adapter = $elementSelect2.data().select2.dataAdapter;
    $elementSelect2.children().each(function () {
        if (!$(this).is('option') && !$(this).is('optgroup')) {
            return true;
        }
        data.push(adapter.item($(this)));
    });
    return data;
}
Aguayo answered 28/12, 2016 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.