Svelte Form Bindings

Forms collect information from visitors, and Svelte makes that job short. The bind directive links an input to a variable in both directions. Typing updates the variable, and changing the variable updates the input.

Text And Number Inputs

The bind value directive handles text fields, text areas, and number fields.

<script>
  let name = "";
  let age = 18;
</script>

<input bind:value={name} placeholder="Your name" />
<input type="number" bind:value={age} />

<p>{name} is {age} years old.</p>

A number input produces a real number, not a string. Svelte converts the value for you.

Two-Way Binding Diagram

   Visitor types "Asha"
          |
          v
   <input bind:value={name}>
          |   ^
          v   |
     let name = "Asha"
          |
          v
   <p>Hello {name}</p> shows "Asha"

   Code sets name = "Ravi"
          |
          v
   Input field shows "Ravi"

Checkboxes

A single checkbox binds to a boolean through the checked property.

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

<label>
  <input type="checkbox" bind:checked={agreed} />
  I accept the terms
</label>

<button disabled={!agreed}>Continue</button>

Radio Groups

The bind group directive connects several radio buttons to one variable. The variable holds the value of the selected button.

<script>
  let size = "medium";
</script>

<label><input type="radio" bind:group={size} value="small" /> Small</label>
<label><input type="radio" bind:group={size} value="medium" /> Medium</label>
<label><input type="radio" bind:group={size} value="large" /> Large</label>

<p>Selected size: {size}</p>

Checkbox Groups

Checkboxes that share a group build an array of the checked values.

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

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

<p>Chosen: {toppings.join(", ")}</p>

Select Menus

A select element binds like a text field. Add the multiple attribute and the variable becomes an array.

<script>
  let country = "India";
</script>

<select bind:value={country}>
  <option>India</option>
  <option>Canada</option>
  <option>Brazil</option>
</select>

Choosing The Right Binding

Input Type          Directive         Variable Holds
------------------  ----------------  ----------------------
Text, textarea      bind:value        String
Number, range       bind:value        Number
Single checkbox     bind:checked      True or false
Radio buttons       bind:group        Selected value
Checkbox group      bind:group        Array of values
Select              bind:value        Selected option

Text Areas And Range Sliders

A textarea binds with the same value directive. A range slider returns a number, so it pairs well with live previews such as a font size picker.

<script>
  let bio = "";
  let fontSize = 16;
</script>

<textarea bind:value={bio} rows="3"></textarea>
<input type="range" min="12" max="32" bind:value={fontSize} />

<p style="font-size: {fontSize}px">{bio.length} characters typed</p>

A Small Combined Form

Several bindings live happily in one component. Each field owns one variable, and a single reactive line summarizes them all.

<script>
  let fullName = "";
  let plan = "free";
  let newsletter = false;

  $: summary = `${fullName || "Guest"} chose the ${plan} plan` +
    (newsletter ? " and joined the newsletter" : "");
</script>

<input bind:value={fullName} placeholder="Full name" />
<select bind:value={plan}>
  <option value="free">Free</option>
  <option value="pro">Pro</option>
</select>
<label><input type="checkbox" bind:checked={newsletter} /> Newsletter</label>

<p>{summary}</p>

The summary line updates after every keystroke or click. No submit button or extra handler participates in that update.

A Layman Analogy

Picture two walkie-talkies tuned to the same channel. Whatever one person says reaches the other person immediately, and replies travel back along the same path. The input and the variable stay in constant conversation.

Common Mistakes To Avoid

  • Using bind:value on a checkbox rather than bind:checked
  • Giving radio buttons no value attribute
  • Starting a checkbox group with a string rather than an array
  • Skipping label elements, which hurts accessibility

Key Points

  • Binding links inputs and variables in both directions
  • Number inputs return real numbers
  • Groups collect radio and checkbox choices
  • Labels make forms usable for everyone