question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default value not working in select

See original GitHub issue

The default select value is not applied to the select widget.

<script>
  var users = {
    1: {name: "aaa"},
    2: {name: "bbb"},
    3: {name: "ccc"},
  };
  var user_id=2;
</script>
 
<select value={user_id}>
  {#each Object.entries(users) as [k, v]}
    <option value={k}>{v.name}</option>
  {/each}
</select>

https://svelte.dev/repl/895418a4cb0249a1af034cfbb71e7ae6?version=3.24.0

I expect bbb to be selected.

Chrome Version 83.0.4103.116 (Official Build) (64-bit) Windows 7

It’s strange, not too severe.

If I change the type of user_id to string it works, but why should I?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
afaurcommented, Jul 17, 2020

@axil

<script>
  import { onMount } from 'svelte'
  import { fetch } from './Fetch.svelte'

  let users = {}, userIds = []	

  let selectedUserId = 5
	
  onMount(async () => {
    const response = await fetch('http://www.site.com/databaseUsers.json')
    users = await response.json()
    userIds = Object.keys(users).map(userId => parseInt(userId, 10))
  })

  $: console.log(selectedUserId)
</script>

<select bind:value={ selectedUserId }>
  { #each userIds as userId }
    <option value={ userId }>{ users[userId].name }</option>
  { /each }
</select>

Fetch.svelte

<script context='module'>
  export const fetch = async url => ({
    async json() {
      return {
        1: { name: "aaa" },
        5: { name: "bbb" },
        7: { name: "ccc" }
      }
    }
  })
</script>
0reactions
axilcommented, Jul 17, 2020

@antony Nevermind! I’m still a big fan of svelte and I appreciate the efforts you make to support and maintain it.

@afaur Actually, in my particular case the indices of the users are taken from the database and can contain gaps when they are deleted.

What about this variant – just one symbol to fix the problem:

<script>
  var users = {
    1: {name: "aaa"},
    2: {name: "bbb"},
    3: {name: "ccc"},
  };
  var user_id=2;
</script>
 
<select value={user_id}>
  {#each Object.entries(users) as [k, v]}
    <option value={+k}>{v.name}</option>
  {/each}
</select>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Default option of select is not showing - Stack Overflow
The default option will be selected if the value attribute of the option tag is set to the default value of the selectedOption...
Read more >
How to set the default value for an HTML <select> element
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean...
Read more >
why default value is not working in select box var... - ServiceNow
i have select box variable o incident form having 3 question choices, out of which i have marked value 3 as default but...
Read more >
Default option in select field is not working - Prismic People
Hello, I have a background select field with two options - white and dark. The "white" option is the default one.
Read more >
[SOLVED] Default value in SELECT field not working
Hello, how to set default value in select field. I added in PA -> fields -> field -> Show in registration + Default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found