Data ordinarily flows down from parent to child. The bind: directive allows data to flow back up, creating two-way bindings between components and DOM elements.
Basic Syntax
When the property name matches the variable name, use shorthand:
Text Input
Bind the value property of text inputs:
Numeric inputs automatically coerce to numbers:
If the input is empty or invalid with type="number", the value is undefined.
Bind checkboxes with bind:checked:
Checkbox Indeterminate State
Checkboxes can be in an indeterminate state:
Group Bindings
Group related inputs with bind:group:
Checkbox Groups
Checkbox groups populate an array:
bind:group only works if the inputs are in the same Svelte component.
Bind file inputs with bind:files:
Select Bindings
Single Select
Bind to the value of the selected option:
Multiple Select
Multiple select populates an array:
Contenteditable Bindings
Bind to contenteditable elements:
Available bindings:
innerHTML
innerText
textContent
Audio/Video Bindings
Two-Way Bindings
currentTime
playbackRate
paused
volume
muted
Readonly Bindings
duration
buffered
seekable
seeking
ended
readyState
played
Video elements have all audio bindings plus:
videoWidth (readonly)
videoHeight (readonly)
Image Bindings
Bind to image dimensions (readonly):
Details Binding
Bind the open property of details elements:
Dimension Bindings
All visible elements support readonly dimension bindings:
Available dimension bindings:
clientWidth
clientHeight
offsetWidth
offsetHeight
contentRect
contentBoxSize
borderBoxSize
devicePixelContentBoxSize
Dimension bindings don’t work on display: inline elements (except elements with intrinsic dimensions like <img> and <canvas>). Change to display: inline-block or another display value.
Element Reference Binding
Get a reference to a DOM node with bind:this:
The value is undefined until the component is mounted. Read it inside effects or event handlers, not during initialization.
Component Bindings
Bind to component props using the same syntax:
Mark props as bindable with $bindable():
Bindable props can have fallback values:
Function Bindings
Use getter/setter functions for validation and transformation:
For readonly bindings, set getter to null:
Inputs revert to default values when forms are reset:
Real-World Examples
Search with Filters
Volume Control