Skip to content Skip to sidebar Skip to footer

Selectize.additem(value) Adds The Value Of The Item And Not The Items Label

I have found plenty of other questions, but all of their answers have been unable to resolve my issue. I am trying to set the value of a selectize.js input to a specified value. H

Solution 1:

Could be that your searchField value needs to be formatted as an array but I am unable to reproduce the display issue you are seeing. Also, could be an issue with how the data is being returned from your remote source. See the working example below that may help you troubleshoot:

// placeholder for data retrieved from remote sourceconst options = [
  { value: 1, text: 'Annie' },
  { value: 2, text: 'Bart' },
  { value: 3, text: 'Carol' }
];

// init selectize inputconst field = $('#select1').selectize({
  options: [],
  placeholder: 'Select...',
  valueField: 'value',
  labelField: 'text',
  searchField: ['text'],
  create: false
});

// change the index to simulate retrieving a different optionconst option = options[0];

// add first option to selectize input and select it
field[0].selectize.addOption(option);
field[0].selectize.addItem(option.value);
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><title>Selectize</title><linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.default.min.css"><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js"></script></head><body><formaction=""method="POST"><inputid="select1"name="select1"type="text" /><buttontype="submit">Submit</button></form></body></html>

Post a Comment for "Selectize.additem(value) Adds The Value Of The Item And Not The Items Label"