A customer types their name into the order field, and the greeting below has to change with every letter. The other way round, when the code changes that name, the field has to change too. A two-way tie like this is called binding, and in Svelte you do it with the bind: directive. If you have used Vue, bind:value is the twin of v-model.
Picture the chalk order board at Mrs. Sri's stall. The customer writes their name on the board, and the cashier reads that same board. If the cashier wipes it and writes something else, the customer sees the new text too. There's no copy, only one board. bind:value makes the input field and its variable one board: the customer writes through the field, the code reads and writes through the variable, and both always hold the same thing.
bind:value: the field and the variable are always the same
This is the example from the first exercise:
<script>
let name = $state("")
</script>
<input bind:value={name} placeholder="Type a name..." />
<p>Hello, {name}!</p>
| Event | Field holds | name |
Shown |
|---|---|---|---|
| Start | (empty) | "" |
Hello, ! |
| Type Sinta | Sinta | "Sinta" |
Hello, Sinta! |
Code runs name = "Budi" |
Budi | "Budi" |
Hello, Budi! |
- First direction: typing in the field changes
nameright away, and the paragraph follows. - Second direction: giving
namea new value from code changes the field's content right away. - Without
bind:,<input value={name} />only goes one way. The field is filled fromname, but typing doesn't changename, so the paragraph staysHello, !. bind:valueis short forvalue={name}plusoninput={(e) => name = e.target.value}.oninputruns every time the field's content changes.eis the event object, soe.target.valueis that content.- The bound variable must be
$state. In a component that uses runes, a plainlet name = ""makes the compiler warn`name` is updated, but is not declared with `$state(...)`, and the paragraph stays atHello, !.
Changing the input while it is typed: function bindings. Since Svelte 5.9 (the exercise editor supports it), bind:value can take two functions: the first reads the value to show in the field, the second stores the field's content. For example, a phone number field that only keeps digits:
<script>
let phone = $state("")
</script>
<input
bind:value={() => phone, (v) => (phone = v.replace(/\D/g, ""))}
placeholder="Phone number"
/>
<p>Saved: {phone}</p>
() => phone reads the value of phone for the field. (v) => (phone = ...) receives the field's content as v, removes everything that isn't a digit, then stores the result in phone. So when the customer types 07700 900-123, phone holds 07700900123, and the field's content changes to 07700900123 too.
Numbers: type="number"
The portion field is written <input bind:value={portions} />, without type="number". The customer types 2, then presses a button that runs portions += 1. Guess: does the field now hold 3 or 21?
A text field's content is always a string, even when what's typed is a number:
| Field | Typed | portions |
After portions += 1 |
|---|---|---|---|
<input bind:value={portions} /> |
2 | "2" (string) |
"21" |
<input type="number" bind:value={portions} /> |
2 | 2 (number) |
3 |
The text field gives "21", because in JavaScript "2" + 1 joins text instead of adding. With type="number" (or type="range", a number slider), Svelte turns the content into a number before storing it, so portions += 1 gives 3. If the number field is emptied, Svelte 5 sets portions to null.
On the stall's order form, the total can be computed directly from that field's content:
<script>
let portions = $state(1)
</script>
<input type="number" bind:value={portions} min="1" />
<p>Total: ${5 * portions}</p>
Checkboxes: bind:checked
A checkbox keeps its state in the checked property: true when it's ticked, false when it isn't. This is the example from the second exercise:
<script>
let agreed = $state(false)
</script>
<label>
<input type="checkbox" bind:checked={agreed} />
I agree
</label>
{#if agreed}
<p>You agree</p>
{:else}
<p>Not agreed yet</p>
{/if}
| Event | agreed |
Shown |
|---|---|---|
| Start | false | Not agreed yet |
| Click the box | true | You agree |
| Click again | false | Not agreed yet |
bind:checkedbinds the tick to a variable. On the stall's form it suits the "Takeaway" option:bind:checked={takeaway}.bind:valueon a checkbox binds itsvalueattribute, not its tick. The box can be ticked, butagreeddoesn't follow the tick.- When the variable is named
checked,bind:checkedalone is enough.
Choices: bind:group
A radio is a round option: in one group, only one can be picked. For a group of radios or checkboxes, use bind:group with the same variable on every option:
<script>
let menu = $state("Fried Rice")
let extras = $state([])
</script>
<label><input type="radio" bind:group={menu} value="Fried Rice" /> Fried Rice</label>
<label><input type="radio" bind:group={menu} value="Chicken Noodles" /> Chicken Noodles</label>
<label><input type="checkbox" bind:group={extras} value="Egg" /> Egg</label>
<label><input type="checkbox" bind:group={extras} value="Crackers" /> Crackers</label>
<p>{menu} + {extras.join(", ")}</p>
- Radios:
menuholds onevalue, for example"Chicken Noodles". Picking another radio replaces it. - Checkboxes:
extrasholds an array of every tickedvalue. If you tick Crackers, then Egg, the result is["Egg", "Crackers"]. The order follows the boxes' order on the page, not the order of your clicks. - The variable for a checkbox group starts as an empty array,
$state([]).
select and textarea
A <select> is a list of options that opens when clicked. <select bind:value={table}> stores the value of the chosen <option>. Written as <option value={2}>Table 2</option>, table holds the number 2, not a string. A <textarea> is a text field with several lines, and <textarea bind:value={note}> works like a plain text field. Both need a $state variable too.
📌 bind:this: a handle on the element
Sometimes you need the DOM element itself, for example to move the cursor into the name field:
<script>
let nameField
function fillName() {
nameField.focus()
}
</script>
<input bind:this={nameField} />
<button onclick={fillName}>Fill in name</button>
bind:this={nameField} stores that <input> element in nameField. This variable becomes a handle on the element, so nameField.focus() moves the cursor into that field. nameField is still undefined when <script> first runs, because the element hasn't been created yet. So use nameField inside a handler, or in onMount (code that runs after the component appears) from the Svelte Lifecycle lesson. This handle isn't shown on the page, so a plain let is enough.
Older code: Svelte 4
In Svelte 4 code, bind:value, bind:checked, bind:group and bind:this are written exactly the same. The difference is that a plain let name = "" is enough for the bound variable, because Svelte 4 tracked a let by itself. Events are also written on:input or on:click. When you move code like that to Svelte 5, wrap those variables in $state.
⚠️ A field that isn't tied to its variable
value={name}withoutbind:. It only goes one way, so typing doesn't changename. In the first exercise, the checker also looks forbind:value.- Numbers from a text field. The content is a string, so
+ 1joins text:"2"becomes"21". Usetype="number". bind:valueon a checkbox. Usebind:checkedfor a single tick, orbind:groupfor a group of options.- A greeting that isn't exact. In the first exercise, the checker reads
Hello, Sinta!: a comma afterHello, one space, and an exclamation mark at the end. In the second exercise, it readsYou agreeandNot agreed yet.
TL;DR: bind:value={name} ties an input field and a $state variable both ways: typing changes the variable, and changing the variable changes the field. Without bind:, value={name} only goes one way. In a component that uses runes, if the variable is made without $state, the text that shows it doesn't change. A text field always gives a string ("2" + 1 becomes "21"), while type="number" gives a number. A single checkbox uses bind:checked (true/false). A group of radios (round options, only one can be picked) uses bind:group with one value, and a group of checkboxes uses bind:group with an array. bind:this stores the DOM element in a variable (a handle) once that element exists. In Svelte 4 code, bind is written the same way, with a plain let. The exercises: let name = $state("") and <input bind:value={name} /> with <p>Hello, {name}!</p> becomes Hello, Sinta!, and let agreed = $state(false) with a box bound by bind:checked={agreed} flips Not agreed yet to You agree.
⚡ bind:value ties both ways
Write:
<script>
let name = $state("")
</script>
<input bind:value={name} placeholder="Type a name..." />
<p>Hello, {name}!</p>
Meaning: bind:value ties the input field and the variable name both ways. Typing in the field changes name right away, and giving name a new value from code changes the field right away. Without bind:, value={name} only goes one way: the field is filled from name, but typing doesn't change name. The variable must be $state.
⚡ A text field is always a string
Write:
<input bind:value={portions} />
Guess first: the customer types 2, then a button runs portions += 1. Does the field hold 3 or 21?
21. A text field's content is always a string, and in JavaScript "2" + 1 joins text instead of adding.
Meaning: For numbers, use <input type="number" bind:value={portions} />. Svelte turns the content into a number first, so portions += 1 gives 3.
⚡ bind:checked for a checkbox
Write:
<label>
<input type="checkbox" bind:checked={agreed} />
I agree
</label>
Meaning: A checkbox keeps its state in checked: true when it's ticked, false when it isn't. bind:checked={agreed} binds that state to a $state variable named agreed. bind:value on a checkbox binds its value attribute, not its tick. A group of radios (round options, only one can be picked) or checkboxes uses bind:group.