GUIDE / FUNDAMENTALS

Forms and two-way binding

Connect input controls to state in both directions, choose the right control for each value, and handle the keyboard in fields.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 09 / 29~3 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What you will learn

  • What bind:value and bind:checked do, and when to use them.
  • Which input control matches each kind of value.
  • How options work in <select>, <radiogroup> and <tabs>.
  • How to react to keys such as Enter and Escape in a field.

bind: keeps a control and a state in step

value={name} sends data one way, from state to the control. bind:value={name} sends it both ways: the control shows the state, and every edit the user makes is written back to it. Use bind:checked for <checkbox> and <switch>, and bind:value for the rest. Anything else that reads the state, such as a summary line, follows the user’s input without any handler.

janel
<script lang="python">
from janelkit import state

name = state("")
size = state("M")
volume = state(40)
agree = state(False)
message = state("Type a name and press Enter.")

def greet(text):
    message.value = f"Hello, {text.strip() or 'friend'}!"
</script>

<window title="A small form" width={480} height={420}>
  <column padding={24} gap={12}>
    <textfield bind:value={name} on:submit={greet} placeholder="Ada" label="Your name" />
    <select options={["S", "M", "L"]} bind:value={size} label="Size" />
    <slider bind:value={volume} min={0} max={100} step={10} label="Volume" />
    <checkbox bind:checked={agree}>I agree</checkbox>
    <text>{name or "Nobody"} · size {size} · volume {volume:.0f} · {"agreed" if agree else "not agreed"}</text>
    <text>{message}</text>
    <button on:click={name = ""; agree = False}>Clear</button>
  </column>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. Each bind: connects one control to one state. Typing, choosing, sliding and ticking all write to state immediately.
  2. The summary line reads the four states, so it changes as the user works.
  3. on:submit={greet} runs when the user presses Enter in the field; greet receives the submitted text.
  4. Clear changes the states from the template. Because the binding goes both ways, the field and the checkbox are cleared too.

What you should see

Typing Ada changes the summary to Ada · size M · volume 40 · not agreed. Enter shows Hello, Ada!. Clear empties the field and unticks the checkbox.

Common mistakes

  • bind: needs writable state. It cannot target a plain variable, a computed value, a prop or a list row variable.
  • checked={agree} on its own is one-way: the checkbox shows agree but does not write to it.

Choose the control by the kind of value

The right control makes the expected value obvious to the user. Pick it by the type of data you need.

You needUseBinding
A short line of text<textfield>bind:value (str)
Several lines of text<textarea>bind:value (str)
Yes or no, in a form<checkbox>bind:checked (bool)
On or off, a setting that applies at once<switch>bind:checked (bool)
A number in a range<slider> (min, max, step)bind:value (number)
One option from a long list<select>bind:value
One option from a few, all visible<radiogroup>bind:value
Which section is shown<tabs>bind:value
Progress of a task<progressbar> (display only)—

Options: plain text or label and value

<select>, <radiogroup> and <tabs> take a list of options. Plain strings are shown and stored as they are. Dictionaries with label and value let the user see a readable label while your Python receives a different value, such as an identifier.

The user’s choice appears immediately. If Python changes the bound state, the control follows Python. <tabs> only draws the bar of tabs: choose the content to show with {#if}.

janel
<script lang="python">
from janelkit import state
tab = state("general")
plan = state(2)
</script>

<window title="Options" width={460} height={300}>
  <column padding={24} gap={12}>
    <tabs options={[{"label": "General", "value": "general"}, {"label": "Billing", "value": "billing"}]} bind:value={tab} />
    {#if tab == "general"}
      <text>General settings</text>
    {:else}
      <radiogroup options={[{"label": "Basic", "value": 1}, {"label": "Pro", "value": 2}]} bind:value={plan} />
      <text>Plan number {plan}</text>
    {/if}
  </column>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

What you should see

General settings is shown first. Choosing Billing shows the plan choice with Pro selected and Plan number 2; choosing Basic shows Plan number 1.

Learn moreConditions and lists ↗

React to keys in a field

<textfield>, <textarea> and <select> also send keyboard and focus events: on:keydown, on:keyup, on:focus and on:blur. The keydown handler receives a KeyEvent whose key is a name such as Enter, Escape or ArrowDown. event.prevent() stops the key from reaching the field, so it is not typed.

TextArea sends on:input only for changes made by the user, not for changes that come from Python.

janel
<!-- cancel and save are script functions; draft is a text state. -->
<textfield bind:value={draft}
  on:keydown={event.key == "Escape" and cancel()}
  on:blur={save()} label="Title" />
Copy

Fragment · Add it to an existing component; it is not a complete file.

How it works

  1. event.key == "Escape" and cancel() calls cancel only when the key is Escape.
  2. on:blur runs when the field loses focus, a common moment to save an edit.

Learn moreAccessibility and keyboard ↗

Give every input a name

A placeholder disappears as soon as the user types, and screen readers may not announce it. Give each input a label, the name a screen reader announces, and when possible a visible <text> above it. Then check the form with the keyboard alone: Tab moves through the controls in template order, Enter submits a field.

Common mistakes

  • Do not rely on the placeholder as the only description of a field.
  • Test both directions of each binding: type in the control, and change the state from a button.

Learn moreAccessibility and keyboard ↗

Key points

  • bind:value and bind:checked connect a control and a state in both directions.
  • Pick the control by the value: text, Boolean, number or one of several options.
  • Options can be strings, or dictionaries with label and value.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.