I was wondering, how could I get the value of the currently selected item in my Selectize.js input? I have checked the documentation and scoured everything selectize.js related on Stackoverflow but found nothing in the way of an example I could go off of. Any ideas? Here is what I figured would work based on the documentation, but instead gave me Undefined is not a function
error.
Please note the very bottom of the code, where I use the select.on('change'
; this (in addition to other API methods) is what I have tried. The on change works perfectly, but unfortunately nothing else has.
var select = $('#searchTextbox').selectize({
maxItems: 1, //Max items selectable in the textbox
maxOptions: 30, //Max options to render at once in the dropdown
searchField: ['text'], //Fields the autocomplete can search through. Example of another searchable field could be 'value'
openOnFocus: true,
highlight: true,
scrollDuration: 60, //currently does nothing; should say how many MS the dropdown and drop up animations take
create: false, //Whether or not the user is allowed to create fields
selectOnTab: true,
render: {
option: function(data, escape) {
var valueArray = [];
valueArray[0] = data.value.split(",");
var color = valueArray[0][0] == "1" ? "green" : "red";
return '<div class="option" style="color: '
+ color
+ ';">'
+ escape(data.text)
+ '</div>';
},
item: function(data, escape) {
var valueArray = [];
valueArray[0] = data.value.split(",");
var color = valueArray[0][0] == "1" ? "green" : "red";
return '<div class="option" style="color: '
+ color
+ ';">'
+ escape(data.text)
+ '</div>';
}
}
});
select.on('change', function() {
var test = select.getItem(0);
alert(test.val());
});