Complete the code to bind the select value to the variable.
<script> let color = 'red'; </script> <select [1]={color}> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select>
In Svelte, to bind the selected value of a <select> element to a variable, use bind:value.
Complete the code to bind the selected option in a multiple select.
<script> let selectedColors = []; </script> <select multiple [1]={selectedColors}> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select>
For multiple select elements, bind:value binds to an array of selected values.
Fix the error in the code to correctly bind the select value.
<script> let fruit = 'apple'; </script> <select [1]={fruit}> <option value="apple">Apple</option> <option value="banana">Banana</option> <option value="orange">Orange</option> </select>
The correct binding for the selected value is bind:value. Other bindings like bind:fruit do not work.
Fill both blanks to bind the select value and handle change event.
<script> let city = 'Paris'; function handleChange(event) { city = event.target.[1]; } </script> <select [2]={city} on:change={handleChange}> <option value="Paris">Paris</option> <option value="London">London</option> <option value="Tokyo">Tokyo</option> </select>
The event target's value property holds the selected option's value. The select element binds with bind:value.
Fill all three blanks to bind a select with options from an array and show the selected value.
<script> let fruits = ['Apple', 'Banana', 'Cherry']; let selectedFruit = fruits[0]; </script> <select [1]={selectedFruit}> {#each fruits as BLANK_2} <option value=[2]>[2]</option> {/each} </select> <p>You selected: {selectedFruit}</p>
Use bind:value to bind the select value. The {#each} block iterates over fruits with variable fruit. The option's value and text use fruit.