How do I set the selectize.js options list programmatically?

Viewed 60191

I know how to set the optionList on Initiliaztion but how do I set it programmatically?

I have an inviteList array:

$("#select-invite").options(inviteList);
3 Answers

I know this is an old question but as of 2021 I have found this to be the simplest way to achieve this:

Building on @byte255's answer above, you only need to use the clearOptions method and addOption method.

let selectize = $("#select-invite")[0].selectize;

selectize.clearOptions();

let newOptions = [
    {
        text: 'Option One',
        value: 1
    },
    {
        text: 'Option Two',
        value: 2
    }
]

selectize.addOption(newOptions);

Related