rosu alin
rosu alin

Reputation: 5830

Selectize, only works the first time. the item function does not work the second time

This is the code that I have for my select script:

 var $select = $('#select-links').selectize({
                theme: 'links',
                maxItems: 1,
                valueField: 'id',
                searchField: 'title',
                options: [
                    {id: 1, title: 'corp.blpnexi.com', preface: 'Primary/Std', value: 'https://corp.blpnexi.com' , secure: 'https://'},
                    {id: 2, title: 'alpha.vidyo.com', preface: 'Secondary/DR', value: 'https://alpha.vidyo.com' , secure: 'https://'},
                ],
                render: {
                    option: function(data, escape) {
                        return '<div class="option">' +
                                '<span class="preface">' + escape(data.preface) + '</span>' +
                                '<span class="secureClass">' + escape(data.secure) + '</span>' +
                                '<span class="title">' + escape(data.title) + '</span>' +
                            '</div>';
                    },
                    item: function(data, escape) {
                        document.getElementById("submit-portal-select").value = data.value;
                        var portal = data.value;
                        $("#portal").val(portal).change();
                        localStorage.portal = portal;
                        console.log("data selected:" + data.title);
                        var classOf = 'titleI2';
                        if(data.title === "corp.blpnexi.com"){
                            classOf = 'titleI';
                        }
                        return '<div class="option">' +
                                '<span class="prefaceI">' + escape(data.preface) + '</span>' +
                                '<span class="divClass">'
                                +'<span class="secureClass">' + escape(data.secure) + '</span>' +
                                '<span class='+ classOf+'>' + escape(data.title) + '</span>' +
                                '</span>'+
                            '</div>';
                    }
                },
                create: function(input) {
                    return {
                        id: 0,
                        title: input,
                        preface: '#',
                        value: null,
                        secure: 'https://'
                    };
                }
            });
            var selectize = $select[0].selectize;
            selectize.setValue(1);

I've set some breakpoints, and tried to debug. When I select a value, it enters the item function. where I set thelocalStorage.portal to the dava.value of that item. This works the first time, but the second time I try to use it, it doesn't enter this code, and it doesn't change, leaving my localStorage to the value that I've set first. How can I make it run that code?or if now, how can I take the correct value when needed?

Upvotes: 0

Views: 683

Answers (1)

rosu alin
rosu alin

Reputation: 5830

I added a callback for onChange and that did it:

  onChange: function(value) {
                    console.log("data selected:" + value);
                    var portal;
                    if(value == 1){
                        portal = "https://corp.blpnexi.com";
                    }else{
                        portal = "https://alpha.vidyo.com";
                    }
                    document.getElementById("submit-portal-select").value = portal;
                    localStorage.portal = portal;
                }

Upvotes: 0

Related Questions