I'm working on a project where every select field is expected to use selectize.js. There can be any number of select fields on the page and I never know so it has to be abstract.
The init is very simple:
$('.selectize').selectize({
create: true
});
You'd think the default selected value of any given select would be used when selectize inits...
<select class="selectize">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3" selected="selected">Option 3</option>
</select>
But for some reason selectize only sets the first option as selected when init runs.
Am I missing something in the docs? Or does selectize really not use the selected attribute from html?
This also doesn't work when using <option value="3" selected>
To clarify, this is a data filtering utility where clicking directional filters, select boxes, etc are expected to reload the page while maintaining all selections. Thus, I have to rely on PHP GET params to set the selected items. That part is working as expected and I have verified that by disabling selectize.
EDIT as requested, this is all standard implementation. Selectize works fine, no console errors. It just inits with only the first value selected.
UPDATE
As answered below, you can use selected but for reasons unknown I can't in my setup. Must be a conflict somewhere. Anyhow, if anyone else has this issue, this is how I resolved mine. On my init, I loop through each select that has a matching get param and set it by comparing against the get value. The sample is here, which is not complete code but enough to get someone running...
var ss = $('#some-select').selectize();
var selectize = ss[0].selectize;
selectize.setValue(selectize.search(get_value).items[0].id);
selected
attribute should work or not, but you can pass an array ofitems
that you want selected by default as one of your init parameters to skin this cat another way - seeitems
under general settings on this page of the docs. – benvc