GUIDE / BUILDING APPLICATIONS

Refs: control an element from code

Reach a specific element from Python to move focus, select text or scroll a list, and react when it appears or disappears.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 17 / 29~2 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

  • When code needs a handle to an element instead of state.
  • How a ref attaches, detaches and reports whether its element exists.
  • Which helpers a ref offers, and their limits.

State describes, refs act

Almost everything on screen follows state: you change data and the interface updates. A few actions are not data but commands to one element: put the cursor in this field, select its text, scroll this list to the end. For those, give the element a ref.

field = ref() in the script creates an empty handle; ref={field} on an element attaches it. field.value is reactive: None until the element exists, the element once it does, and None again when it is destroyed.

MemberWhat it does
focus() / blur()Gives or removes keyboard focus.
select_all()Selects all the text of a <textfield> or <textarea>.
scroll_to_start() / scroll_to_end() / scroll_to(…)Scrolls a <list> or a <scroll>. In a <list>, scroll_to takes a row index; in a <scroll>, the ref of an element inside it, which it brings fully into view. scroll_to_end also resumes following new rows with follow.
existsTrue while the element exists. Reactive.
focusedWhether the element, or something inside it, has focus. Not reactive: read it in a handler.
valueThe underlying Qt Quick item, or None. A Qt interface, not JanelKit’s: last resort only.

Focus a field from a button

The methods do nothing while the element does not exist, so calling them too early fails silently. During setup the view does not exist yet; call them from on_mount or from a handler.

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

name = state("Ada Lovelace")
field = ref()
</script>

<column padding={24} gap={12}>
  <textfield ref={field} bind:value={name} label="Name" />
  <row gap={8}>
    <button on:click={field.focus()}>Edit name</button>
    <button on:click={field.select_all()}>Select all</button>
  </row>
</column>
Copy

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

How it works

  1. ref={field} attaches the handle when the <textfield> is created.
  2. Each button sends one command to that specific field.
  3. name still holds the text; the ref only controls the element.

What you should see

Edit name puts the cursor in the field. Select all selects Ada Lovelace, so typing replaces it.

Learn moreLifecycle and effects ↗

React when an element appears

A component’s on_mount runs once, so it does not run again when a field inside {#if} appears later. The ref knows: exists changes when the element is created or destroyed. An effect that reads it runs at each change, which lets you focus a field every time it appears, with no delays or guesses.

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

editing = state(False)
name = state("Ada")
field = ref()

def focus_when_shown():
    if field.exists:
        field.focus()
        field.select_all()

effect(focus_when_shown)
</script>

<column padding={24} gap={12}>
  <button on:click={editing = not editing}>{"Done" if editing else "Rename"}</button>
  {#if editing}
    <textfield ref={field} bind:value={name} label="Name" />
  {:else}
    <text>Name: {name}</text>
  {/if}
</column>
Copy

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

How it works

  1. The effect reads field.exists, so it reruns when the field is created or destroyed.
  2. Each Rename creates a new <textfield>; the effect focuses it and selects its text.
  3. name belongs to the component, not to the field, so the text survives each Done.

What you should see

Rename shows the field with Ada selected. Type Grace and click Done: Name: Grace. Rename again shows Grace selected.

Limits

A ref points to one element. It is not a way to read values: read state instead, which is reactive and testable.

Common mistakes

  • One ref reused inside {#each} points to the last row created, not to every row.
  • select_all() on an element without text raises TypeError; the scroll helpers only work on <list> and <scroll>.
  • A ref on a component whose root is a box targets that root, not an input inside the component.
  • Prefer the helpers to ref.value: Qt’s item API is not part of JanelKit’s contract and may change.

Learn moreLarge lists ↗

Key points

  • Use state for what is shown and refs for commands to one element.
  • A ref is None until its element exists; exists is reactive.
  • focus, blur, select_all and the scroll helpers do nothing while the element is missing.

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