Attributes

Data attributes are evaluated in the order they appear in the DOM, have special casing rules, can be aliased to avoid conflicts with other libraries, can contain Datastar expressions, and have runtime error handling.

The Datastar VSCode extension and IntelliJ plugin provide autocomplete, diagnostics, hover documentation, and syntax highlighting.

data-attr #

Sets the value of any HTML attribute to an expression, and keeps it in sync.

1<div data-attr:aria-label="$foo"></div>

The data-attr attribute can also be used to set the values of multiple attributes on an element using a set of key-value pairs, where the keys represent attribute names and the values represent expressions.

1<div data-attr="{'aria-label': $foo, disabled: $bar}"></div>

data-bind #

Creates a signal (if one doesn’t already exist) and sets up two-way data binding between it and an element’s current bound state. When the signal changes, Datastar writes that value to the element. When one of the bind events fires, Datastar reads the element’s current bound property/value and writes that back to the signal.

The data-bind attribute can be placed on any HTML element on which data can be input or choices selected (input, select, textarea elements, and web components). Native elements use their built-in bind semantics automatically. Generic custom elements default to binding through value and listening on change.

data-bind does not inspect the event payload. It only uses the configured event as a signal to re-read the element’s current bound property/value. If you need to pull data from event itself, use data-on:* instead.

1<input data-bind:foo />

The signal name can be specified in the key (as above), or in the value (as below). This can be useful depending on the templating language you are using.

1<input data-bind="foo" />

Attribute casing rules apply to the signal name.

1<!-- Both of these create the signal `$fooBar` -->
2<input data-bind:foo-bar />
3<input data-bind="fooBar" />

The initial value of the signal is set to the value of the element, unless a signal has already been defined. So in the example below, $fooBar is set to baz.

1<input data-bind:foo-bar value="baz" />

Whereas in the example below, $fooBar inherits the value fizz of the predefined signal.

1<div data-signals:foo-bar="'fizz'">
2    <input data-bind:foo-bar value="baz" />
3</div>

Predefined Signal Types

When you predefine a signal, its type is preserved during binding. Whenever the element’s value changes, the signal value is automatically converted to match the original type.

For example, in the code below, $fooBar is set to the number 10 (not the string "10") when the option is selected.

1<div data-signals:foo-bar="0">
2    <select data-bind:foo-bar>
3        <option value="10">10</option>
4    </select>
5</div>

In the same way, you can assign multiple input values to a single signal by predefining it as an array. In the example below, $fooBar becomes ["fizz", "baz"] when both checkboxes are checked, and ["", ""] when neither is checked.

1<div data-signals:foo-bar="[]">
2    <input data-bind:foo-bar type="checkbox" value="fizz" />
3    <input data-bind:foo-bar type="checkbox" value="baz" />
4</div>

File Uploads

Input fields of type file will automatically encode file contents in base64. This means that a form is not required.

1<input type="file" data-bind:files multiple />

The resulting signal is in the format { name: string, contents: string, mime: string }[]. See the file upload example.

If you want files to be uploaded to the server, rather than be converted to signals, use a form and with multipart/form-data in the enctype attribute. See the backend actions reference.

Modifiers

Modifiers allow you to modify behavior when binding signals using a key.

Native form controls use their built-in binding semantics automatically. Generic custom elements default to value and change. Use __prop and __event when a custom element’s live state is stored somewhere else.

1<my-toggle data-bind:is-checked__prop.checked__event.change></my-toggle>

data-class #

Adds or removes a class to or from an element based on an expression.

1<div data-class:font-bold="$foo == 'strong'"></div>

If the expression evaluates to true, the font-bold class is added to the element; otherwise, it is removed.

The data-class attribute can also be used to add or remove multiple classes from an element using a set of key-value pairs, where the keys represent class names and the values represent expressions.

1<div data-class="{success: $foo != '', 'font-bold': $foo == 'strong'}"></div>

Modifiers

Modifiers allow you to modify behavior when defining a class name using a key.