DocsState & inputs · FQL
On this page

Bind inputs and defaults

Connect events to state and prefill editable fields.

Before you begin: Declare page state.

State from Imported HTML

When a page uses f-insert or f-replace, imported files can also declare state in a {STATE} comment inside their own <head>.

index.html

Frontbacked resolves imports before serving the page. If an imported file is a full HTML page, only its <body> content is inserted, but its head state is merged into the main page's single {STATE} comment. Imported state entries are placed before the main page state entries, so the main page can override a duplicate key.

State from DOM Events

The most common state source is a DOM event.

FQL state configuration

Add this configuration inside your page’s {STATE} declaration. Inspect state shows its configuration; live queries and saved data run against your local site. Try a complete FQL page →

The syntax is:

text

Examples:

SourceMeaning
#email.oninput.target.valueOn input, read event.target.value.
#terms.onchange.target.checkedOn change, read checkbox checked state.
#form.onsubmitOn submit, pass the event object.
#file.onchange.target.files[0]On change, read the first selected file.

Event names are written as oninput, onchange, onclick, onsubmit, and so on. FQL removes the on prefix when binding the actual DOM event.

One binding for multiple elements

A selector-based event source is attached to every element that matches the selector, not only the first match. This includes controls added by f-list, so newly loaded options and refreshed lists keep their event bindings. Use it for option buttons, segmented controls, and repeated inputs:

index.html

Use currentTarget when the value should come from the element matched by the selector—the button whose listener is handling the event. Use target when the value should come from the element that originated the event; these can differ when a matched element contains a nested icon or label. In the example above, clicking any of the three buttons updates transferType with that button's value.

Default Values

Put a literal value in parentheses after a state key to set its default.

FQL state configuration

Add this configuration inside your page’s {STATE} declaration. Inspect state shows its configuration; live queries and saved data run against your local site. Try a complete FQL page →

Supported default literals are:

LiteralExample
Boolean"terms(true)"
Number"quantity(1)"
String"name('Guest')"
Null"coupon(null)"

The actual state key is the part before the default. "terms(true)" creates $state.terms with an initial value of true.

Prefill editable state from saved data

Use an expression inside the state key's parentheses to prefill a form when its data arrives. Bind the control to that editable state:

index.html

The saved value supplies the default until the visitor edits it. Input events and Frontbacked.state.set() then keep the edited value, including an intentionally empty string, false, or 0. Use the same state in labels, swatches, and selection controls so the whole form stays in sync.

Continue with Keep data live.