This documentation described how to initialize select: http://next.materializecss.com/select.html,
var instance = M.Select.init(elem, options);
How can I pass the default value to the options? React does not allow selected in
If using React, let's say you had 3 options:
\
\
Then your \
<select defaultValue="5">
Otherwise in normal HTML you could just apply "selected" to one of the options.
Hey,
Like this:
<select defaultValue="Option 1">
<option defaultValue disabled>Option 1</option>
<option defaultValue="Option 2">Option 2</option>
</select>
@weasteam please let us know if these solutions fixed your issue
@Dogfalo no, it is not resolved.
It looks like the M.Select.init() will pick up the option with selected option. However, the <select default='xxx'> would not add selected to any options and therefore, M.Select.init() could not find the default value.
Here is the workflow:
<select defaultValue='xxx'>...</select>selected attribute to <options>M.Select.init() to init the materialize select.M.Select.init() is trying to set the <option selected> as default. Since this attribution is not here, as point 2 mentioned. It could not set the default value.I would propose a solution like M.Select.init(elem, {default: "option 1"})
Most helpful comment
@Dogfalo no, it is not resolved.
It looks like the
M.Select.init()will pick up theoptionwithselectedoption. However, the<select default='xxx'>would not addselectedto any options and therefore,M.Select.init()could not find the default value.Here is the workflow:
<select defaultValue='xxx'>...</select>selectedattribute to<options>M.Select.init()to init the materialize select.M.Select.init()is trying to set the<option selected>as default. Since this attribution is not here, as point 2 mentioned. It could not set the default value.I would propose a solution like
M.Select.init(elem, {default: "option 1"})