Skip to content

Select Statement Initial Value Not Working #3060

@stevesob

Description

@stevesob

I am using the "select" statement to create a control and it works great with bind:value except that I can't seem to set an initial value.

Here is the control, when calling from the parent I need the select to be on the selected option, and it always selects the first option.

<script>
  export let selected
  export let label = ''
  let colors = [
    'w3-red',
    'w3-pink',
    'w3-purple',
    'w3-deep-purple',
    'w3-indigo',
    'w3-blue',
    'w3-light-blue',
    'w3-cyan',
  ]
</script>

<p>
  <span class="w3-text-grey w3-margin-right">{label}</span>
  selected={selected}
  <select class="w3-select {selected}" bind:value={selected}>
    {#each colors as color (color)}
      <option class={color}>{color}</option>
    {/each}
  </select>

</p>

Parent calling

<script>
 import W3selectcolor from '../components/ui/W3selectcolor.svelte'
 let selected = 'blue'
</script>
<W3selectcolor label="Select Color:" bind:selected />

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions