Select2 : multi-select : get selected values/text : data array

Retrieve the selected options in an object array. **Need to figure out how to split/format this to get just the text...

by john_s

HTML

<script src="http://ivaynberg.github.com/select2/select2-master/select2.js"></script>
<link rel="stylesheet" href="http://ivaynberg.github.com/select2/select2-master/select2.css">
<input id="test" width="200" />

CSS

p { margin: 1em 0; }

JavaScript

var test = $('#test');


$(test).select2({
    multiple: true,
    tokenSeparators: [","],
    minimumInputLength: 1,
    ajax: {
        url: "getTags.php",
        dataType: 'json',
        data: function (term) {
            return {
                q: term
            };
        },
        results: function (data) {
            return { results: data };
        }
    },
    initSelection: function (element, callback) {
        var data = [];
        $(element.val().split(',')).each(function () {
            data.push({id: this, text: this});
        });
        callback(data);
    }
});

$(test).val(["test1","test2"]).trigger("change");