Form Bindings

Loading concept...

🎯 Svelte Form Bindings: The Magic Glue Between You and Your App

Imagine you have a magical notebook. Whatever you write in it appears on a big screen. And if someone changes the screen, your notebook updates too! That’s exactly what form bindings do in Svelte.


🧵 The One Analogy: The Two-Way Mirror

Think of Svelte bindings like a two-way mirror in a toy store:

  • You stand on one side (your variable)
  • Your friend stands on the other side (the input field)
  • Whatever face you make, they see it instantly
  • Whatever face they make, YOU see it instantly too!

This is called two-way data binding. No delays. No messengers. Just instant sync!


📝 Text Input Binding

What Is It?

A text input is like a blank name tag. You type your name, and Svelte remembers it immediately.

How It Works

<script>
  let name = '';
</script>

<input bind:value={name}>
<p>Hello, {name}!</p>

The Magic Explained

  • bind:value={name} creates the two-way mirror
  • Type in the box → name updates
  • Change name in code → the box updates too!

Real Life Example

  • Filling out your username on a website
  • Typing a search query
graph TD A[You Type] --> B[Input Box] B --> C[bind:value] C --> D[Variable Updates] D --> E[Screen Shows New Value]

🔢 Numeric Input Binding

What Is It?

Sometimes you need numbers, not words. Like counting your toys or setting your age.

How It Works

<script>
  let age = 10;
</script>

<input type="number" bind:value={age}>
<p>You are {age} years old!</p>

The Clever Part

  • Svelte automatically converts the text to a real number
  • "25" becomes 25 (no quotes!)
  • You can do math directly: {age + 5}

Why This Matters

Without Svelte’s magic, form inputs always give you text. Svelte does the hard work for you!


📄 Textarea Binding

What Is It?

A textarea is like a big sticky note. It’s for longer messages—stories, poems, or secret plans!

How It Works

<script>
  let story = 'Once upon a time...';
</script>

<textarea bind:value={story}></textarea>
<p>{story}</p>

It’s the Same Magic!

  • Works exactly like text input binding
  • Just more room for bigger thoughts
  • Every keystroke updates your variable

Real Life Example

  • Writing an email
  • Filling out a feedback form

☑️ Checkbox Binding

What Is It?

A checkbox is like a light switch. It’s either ON or OFF. Yes or No. True or False.

How It Works

<script>
  let hungry = false;
</script>

<label>
  <input type="checkbox" bind:checked={hungry}>
  I am hungry
</label>

{#if hungry}
  <p>🍕 Let's eat pizza!</p>
{/if}

Notice the Difference!

  • We use bind:checked instead of bind:value
  • The variable is a boolean (true or false)
  • Check the box → variable becomes true
  • Uncheck → variable becomes false
graph TD A[Checkbox Unchecked] -->|Click| B[hungry = true] B -->|Click| A A --> C[false] B --> D[true]

🔘 Radio Button Binding

What Is It?

Radio buttons are like multiple choice questions. You can only pick ONE answer!

How It Works

<script>
  let flavor = 'vanilla';
</script>

<label>
  <input type="radio"
         bind:group={flavor}
         value="vanilla">
  Vanilla
</label>

<label>
  <input type="radio"
         bind:group={flavor}
         value="chocolate">
  Chocolate
</label>

<label>
  <input type="radio"
         bind:group={flavor}
         value="strawberry">
  Strawberry
</label>

<p>You chose: {flavor}</p>

The Key Insight

  • All radios share the same variable via bind:group
  • Each has a different value
  • Clicking one sets the variable to that value

🔗 bind:group - The Team Captain

What Is It?

bind:group is like a team captain who keeps track of who’s on the team.

For Radio Buttons (Pick One)

<script>
  let pet = 'dog';
</script>

<input type="radio" bind:group={pet} value="dog"> Dog
<input type="radio" bind:group={pet} value="cat"> Cat
  • Only ONE can be selected
  • Variable holds a single value

For Checkboxes (Pick Many)

<script>
  let toppings = [];
</script>

<label>
  <input type="checkbox"
         bind:group={toppings}
         value="cheese">
  Cheese
</label>

<label>
  <input type="checkbox"
         bind:group={toppings}
         value="pepperoni">
  Pepperoni
</label>

<label>
  <input type="checkbox"
         bind:group={toppings}
         value="mushrooms">
  Mushrooms
</label>

<p>Toppings: {toppings.join(', ')}</p>
  • MULTIPLE can be selected
  • Variable is an array of values
graph TD A[bind:group] --> B{Input Type?} B -->|Radio| C[Single Value] B -->|Checkbox| D[Array of Values] C --> E[pet = 'dog'] D --> F["toppings = ['cheese', 'mushrooms']"]

📋 Select Binding

What Is It?

A dropdown menu. Like choosing your favorite game from a list.

How It Works

<script>
  let color = 'blue';
</script>

<select bind:value={color}>
  <option value="red">Red</option>
  <option value="blue">Blue</option>
  <option value="green">Green</option>
</select>

<p style="color: {color}">
  Your favorite: {color}
</p>

Simple Rules

  • Use bind:value on the <select> tag
  • Each <option> has a value
  • Picking an option updates your variable

Real Life Example

  • Choosing your country
  • Selecting a shipping method

📋📋 Multiple Select Binding

What Is It?

Same as a select, but you can pick MULTIPLE items! Like choosing all the sports you like.

How It Works

<script>
  let sports = [];
</script>

<select multiple bind:value={sports}>
  <option value="soccer">Soccer</option>
  <option value="basketball">Basketball</option>
  <option value="tennis">Tennis</option>
  <option value="swimming">Swimming</option>
</select>

<p>You like: {sports.join(', ')}</p>

Key Differences

  • Add the multiple attribute
  • Variable must be an array (use [])
  • Hold Ctrl/Cmd to select multiple items

🎯 Quick Comparison Table

Binding Type Directive Variable Type Use Case
Text Input bind:value string Names, emails
Number Input bind:value number Age, quantity
Textarea bind:value string Long text
Checkbox bind:checked boolean Yes/No choice
Radio Button bind:group string Pick one
Checkbox Group bind:group array Pick many
Select bind:value string Dropdown pick one
Multiple Select bind:value array Dropdown pick many

🚀 Why This Matters

Without bindings, you’d have to:

  1. Listen for every change event
  2. Manually grab the input value
  3. Update your variable yourself
  4. Hope you didn’t miss anything!

With Svelte bindings:

  1. Write bind:value or bind:checked
  2. Done! Everything stays in sync automatically.

🧙‍♂️ The Golden Rules

  1. Text/Number/Textarea → Use bind:value
  2. Checkbox (single) → Use bind:checked
  3. Radio buttons → Use bind:group with a string variable
  4. Checkbox group → Use bind:group with an array
  5. Select → Use bind:value
  6. Multiple select → Use bind:value with an array

🎉 You Did It!

You now understand how Svelte creates that magical two-way mirror between your code and your forms. Every input becomes effortlessly connected to your data.

Remember: Binding is just Svelte’s way of saying “whatever happens here, show it there, and vice versa.”

Go build something amazing! 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.