How to use .setValue() ?
#1965
-
|
Want to programmatically change the selected First I initialize many jQuery('.booleanWithDependancies select').selectize({
plugins : ["clear_button"]
});Note: I'm not storing the result in a variable. Perhaps I need to? Later, I try to grab one instance like this: var $selectizeInstance = jQuery( '#' + variable ).selectize;... where function selectize(settings_user)I found documentation for getting an instance of a Having seemingly got a $selectizeInstance.setValue([ 43 ]);results in: Uncaught TypeError: jQuery(...).selectize.setValue is not a functionTried to finding clues via the only <select id="select-tools" multiple placeholder="Pick a tool..."></select>var $select = $('#select-tools').selectize({
maxItems: null,
valueField: 'id',
labelField: 'title',
searchField: 'title',
options: [
{id: 1, title: 'Spectrometer', url: 'http://en.wikipedia.org/wiki/Spectrometers'},
{id: 2, title: 'Star Chart', url: 'http://en.wikipedia.org/wiki/Star_chart'},
{id: 3, title: 'Electrical Tape', url: 'http://en.wikipedia.org/wiki/Electrical_tape'}
],
create: false
});var control = $select[0].selectize;
$('#button-setvalue').on('click', function() {
control.setValue([2, 3]);
});However the 'event' I am using is the response from an XHR request. So all I really need to do, or so I think/thought, is to get a Does anyone have some insight on how to do this, perhaps where I am going wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
UPDATE: I figured this one out. It does seem that, as suspected, I needed to 'trap' (AKA assign) each instance I was creating with the jQuery selection returning many jQuery('.booleanWithDependancies select').each( function ( idx , el ) {
window[ '$selectize' + el.name ] = jQuery( el ).selectize({
plugins : ["clear_button"]
});
})Apparently Later ... jQuery.ajax({
url : 'scriptReturningJSON',
data : {
item : ev.currentTarget.value
}
}).done( function ( rspnse ) {
if ( typeof rspnse[0] !== 'undefined' ) {
jQuery.each( rspnse , function ( idx , el ) {
const selectIdSuffixString = rspnse[ idx ].variety;
const optionValueInteger = rspnse[ idx ].id;
var control = window[ '$selectize' + selectIdSuffixString ][0].selectize;
control.setValue( optionValueInteger )
}
});
}
});I hope this helps someone in the future. |
Beta Was this translation helpful? Give feedback.
UPDATE: I figured this one out.
It does seem that, as suspected, I needed to 'trap' (AKA assign) each instance I was creating with the jQuery selection returning many
selectelements asselectizeinstances. This involved the following:Apparently
window['$selectize' + el.name ]is the same as creating a dynamic, unique variable on the fly usingeval(). Having memory burn of whispers thateval()is a performance hit, I went withwindow[]instead.Later ...