Skip to content

Instantly share code, notes, and snippets.

@nikitapashinsky
Last active February 23, 2023 07:32
Show Gist options
  • Save nikitapashinsky/46bd59f899cde25f1a95cb9f8cb2aace to your computer and use it in GitHub Desktop.
Save nikitapashinsky/46bd59f899cde25f1a95cb9f8cb2aace to your computer and use it in GitHub Desktop.
Svelte radio group rendered within the #each block
<script>
/*
This is an adapted example where radio buttons are rendered from an array.
I removed everything related to flavours for brevity (use {selectedScoops} instead of {scoops})
https://svelte.dev/examples/group-inputs
*/
let scoops = [
{ value: 1, text: "one" },
{ value: 2, text: "two" },
{ value: 3, text: "three" }
]
// Make first radio button checked by default
let selectedScoops = scoops[0].value;
</script>
{#each scoops as scoop}
<label>
<input
type=radio
name="scoops"
bind:group={selectedScoops}
value={scoop.value}
>
{scoop.text} {scoop.value === 1 ? 'scoop' : 'scoops'}
</label>
{/each}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment