Svelte: bind:value for keyed select tag doesn't work when options are updated

Created on 2 Oct 2018  路  8Comments  路  Source: sveltejs/svelte

The selected value is known oncreate, but the <option> values are added later. The selected value isn't set when they are added.

https://svelte.technology/repl?version=2.13.5&gist=17b698af0622793d4ab86ae62e275a51

Looks like it's related/similar to #1730 #1252 #1711

bug

Most helpful comment

@aubergene Hi! A quick workaround for this issue:
https://svelte.technology/repl?version=2.13.5&gist=4f15953652df80e9924a652e8865c8cd

<option selected={selected === item.slug} value="{item.slug}">{item.name}</option>

All 8 comments

yep, it's definitely my #1730 case.

As a horrid hack I've got this, any improvements most welcome

edit: solution below is much better, thanks!

@aubergene Hi! A quick workaround for this issue:
https://svelte.technology/repl?version=2.13.5&gist=4f15953652df80e9924a652e8865c8cd

<option selected={selected === item.slug} value="{item.slug}">{item.name}</option>

I also update my example to v3: https://svelte.dev/repl/a2e1ae9fd08640d494b892a135df3c8f?version=3

@Rich-Harris maybe you know, have any chance to change browser API here?

At the same time as fixing this, we should also make it a compile-time error to specify selected on an <option> inside a <select> that is one- or two-way bound. That will unfortunately mean that the necessary workaround would then refuse to compile, but it seems like the sanest way to maintain a consistent state.

Hello :wave: Is there any update on this issue, or possible v3 workarounds in the interim?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lnryan picture lnryan  路  3Comments

Rich-Harris picture Rich-Harris  路  3Comments

plumpNation picture plumpNation  路  3Comments

robnagler picture robnagler  路  3Comments

bestguy picture bestguy  路  3Comments