Selectize.js: Setting a default value from <select> element itself

Created on 2 Jan 2014  Â·  12Comments  Â·  Source: selectize/selectize.js

Hi again - based on your "Movies" example, I have a [select] box pulling in remote data. What's the best way to set a default value? Could it happen using the HTML element itself, it would it need to happen from within the selectize options object?

Most helpful comment

Easiest way I've found is to grab the selectize instance, and use the setValue to set the selected option.

E.g., $('.selectize')[0].selectize.setValue('1')
(whereby 1, in this example, is the option value of the item I want selected)

All 12 comments

I'm having trouble with this too. Is there a way to just set the first option as selected?

I am a newbie and might have not seen something obvious, but i have the same problem: I need to set default values (for an edit view) and i haven't found any solution. I know it would be possible with some JS (addOption, setValue) but this is not a nice way. Like @ryanintn proposed, would be great if one could just use the first option as a default value

I need the saved database value to be loaded from remote in an edit form. There is a custom renderer, so [name, id] pair is not working out. This alone could be a deal-breaker for Selectize.

anyone found out how to set a default value?

onLoad: function(value){
// set default ;
}

?

2014-09-16 15:37 GMT+02:00 Vivian Guillen [email protected]:

anyone found out how to set a default value?

—
Reply to this email directly or view it on GitHub
https://github.com/brianreavis/selectize.js/issues/236#issuecomment-55742906
.

Pozdrawiam,
Sławomir Gajowniczek

Easiest way I've found is to grab the selectize instance, and use the setValue to set the selected option.

E.g., $('.selectize')[0].selectize.setValue('1')
(whereby 1, in this example, is the option value of the item I want selected)

@dule

The problem with this approach is that onChange event is fired.

I need to load the page, set some initial value inline on a