select2 replace data code example

Example 1: select2 replace options

var options = [];
$.each(dataReturn, function (i, obj) {
    //console.log(i);
    //console.log(obj);
    options.push({
        text: obj.NickName,
        id: obj.NickName
    });
})
$("#BeneType").empty().select2({
    data: options
});

Example 2: select2 replace options

var initQuantitiesDropdown = function () {
    var options = [];
    var selectedFruit = $("#fruits").val();
    $.each(pageData.products[selectedFruit].quantities, function (key, value) {
        options.push({
            text: value,
            id: key
        });
    })
    $("#quantities").empty().select2({
        data: options
    });
};

$("#fruits").select2().change(initQuantitiesDropdown);
initQuantitiesDropdown();

Tags:

Misc Example