How to add item into selectize js automatically after document ready?



I have selectize js code. It runs well and shows right option after i type key on select box.

Here is the code :

        maxItems: diffItems,
        valueField: 'nick_name',
        labelField: 'full_name',
        searchField: ['nick_name', 'full_name'],
        options: [],
        create: false,
        render: {
            item: function(item, escape) {
                return '<div id="' + escape( + '">' +
                    '<span class="nick_name">' + escape(item.full_name) + '</span>' +
                    '<span class="full_name"> &lt;' + escape(item.nick_name) + '&gt;</span>' +
            option: function(item, escape) {
                return '<div class="box-user clearfix">' +
                      '<div class="thumb col-xs-1 mr10">' +
                          '<img class="lazyload" src="'+escape(item.avatar)+'" data-src="' + escape(item.avatar)+'">' +
                      '</div>' +
                      '<div class="col-xs-8">' +
                          '<span class="label" style="color: #000;">' + escape(item.full_name) + '</span>' +
                          '<span class="caption">/' + escape(item.nick_name) + '</span>' +
                      '</div>' +
        load: function(query, callback) {
            if (!query.length) return callback();
                url: '/ajax/origin/search',
                type: 'GET',
                dataType: 'json',
                data: {
                    q: query
                error: function() {
                success: function(res) {

But, i got the problem.

After document ready, i want selectize add item automatically after document / page has already loaded without i have to type the key on select box. How is the code?

        if(diffItems != 49) {

        // This is the code's place to add item automatically and the key is $_POST['nick']

Thank you.
Sorry for bad english





You can add options to existing selectize by following approach:

    var selectize_element = $("#select-links")[0].selectize;
        text:'First Item',
        value: 'First Item'
    selectize_element.refreshOptions() ;
    selectize_element.addItem('First Item');


Facebook Comments

Post a comment