Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<select value> does not select correct option on first render when using one-way binding #1252

Closed
romuloab opened this issue Mar 17, 2018 · 6 comments

Comments

@romuloab
Copy link

When specifying a value for a <select>, the element does not select correct option upon rendering for the first time. It always select the first available <option>, regardless of that option's value. After the first render, if I change the state, then the correct option gets selected.

Steps to reproduce:

  1. Create a component:
<select :value>
  <option>foo</option>
  <option>bar</option>
</select>
  1. Render it:
const sel = new Select({target: ..., data: {value: 'bar'}});

Expected behavior:

  • The component shows the option "bar" selected.

Observed behavior:

  • The component shows the option "foo" selected, even though state.value is "bar".

If I change <select :value> to <select bind:value>, the correct option gets selected.

REPL:

User Agents:

  • Chrome 64
  • Firefox 60
  • Safari 11.0.3 (13604.5.6)
@arxpoetica
Copy link
Member

That's because bind:value and :value actually do different things. I was confused about this at first too. See: https://svelte.technology/guide#two-way-binding

@romuloab
Copy link
Author

I'm aware they do different things, I just mentioned that bind:value does the thing I was expecting -- to show the option that matches the value attribute, but I don't want to use two-way binding.

In pure HTML I'd have <select><option>foo</option><option selected>bar</option></select>. Perhaps for the one-way-binding version I have to write like this:

<select>
  {{#each items as item}}
    <option selected="{{item===value}}">{{item}}</option>
  {{/each}}
</select>

Is this the official way to approach this problem without resorting to two-way binding? Or should <select :value> behave the same as <select bind:value> for the initial render (meaning: when I load the page, I see the correct value being displayed without interacting with the select element).

@okane103
Copy link

okane103 commented Mar 24, 2018

<select :value>
  <option>foo</option>
  <option selected>bar</option>
</select>

This is working for me, with the 'selected' attr on the final option. I tried to link the REPL I was using but it won't save. When I change the bottom right JSON back and forth:

{
"value": "foo"    
}

and

{
"value": "bar"    
}

The drop-down is changing for me.

@sp90
Copy link

sp90 commented Aug 7, 2018

@okane103 Even tough you get the right solution shouldn't it be solving it via :value

@Rich-Harris
Copy link
Member

@xmlking
Copy link

xmlking commented Jan 18, 2023

This works in v3, so I'll close this: https://svelte.dev/repl?version=3.0.0-beta.28&gist=985e5864be8a9887350b2ab4ca2a220f

will this work with Server Side Rendering (SSR) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants