sercrod

:value

Summary

:value is an attribute binding directive that controls the value property and value attribute of a form control. It evaluates a Sercrod expression and writes the result into both the DOM property el.value and the value attribute for form elements, keeping the control’s visible value in sync with data.

For non form elements, :value behaves like a normal colon attribute binding and only updates the value attribute.

Basic example

Prefilling a text input from data:

<serc-rod id="app" data='{
  "form": { "name": "Alice" }
}'>
  <form method="post" action="/submit">
    <label>
      Name:
      <input type="text" name="name" :value="form.name">
    </label>
    <button type="submit">Send</button>
  </form>
</serc-rod>

Behavior:

:value is one way: the DOM property and attribute are driven by data, but editing the input does not automatically update data unless other directives such as *input or n-input are used.

Behavior

Core rules:

Evaluation timing

:value is evaluated during element rendering, as part of the colon attribute binding pass.

High level order:

When the host re renders due to data changes, :value is re evaluated with the updated scope, so the control’s value reflects the latest data.

Execution model

Conceptually, when Sercrod renders an element that has :value:

  1. Sercrod discovers an attribute named :value on the template node.

  2. It extracts the expression string, such as form.name or user.profile.displayName.

  3. It evaluates the expression using eval_expr with:

    • scope as the current scope.
    • ctx containing el (the working element) and mode "attr:value".
  4. It inspects the result:

    • If the result is strictly false, null, or undefined:
      • It removes the value attribute from the element.
      • For form controls, el.value is left as is by this branch.
    • Otherwise:
      • If the element is INPUT, SELECT, TEXTAREA, or OPTION, it sets el.value = String(result).
      • It passes result to the attr filter as attr("value", result, {el, scope}).
      • If the filter returns a pair where pair.value is not null, it sets the attribute:
        • pair.name as the attribute name.
        • A stringified value (empty string if pair.value is true, or String(pair.value) otherwise).
  5. If an exception occurs during expression evaluation, Sercrod sets an empty value attribute as a fallback.

  6. If cleanup.handlers is enabled, Sercrod removes the :value attribute from el, leaving only the concrete value attribute.

Use with structural directives and loops

:value is an attribute binding and therefore composes naturally with structural directives that act at the element or container level.

Examples:

Best practices

Additional examples

Hidden ID field:

<serc-rod id="app" data='{
  "user": { "id": 42, "name": "Alice" }
}'>
  <form method="post" action="/users/save">
    <input type="hidden" name="id" :value="user.id">
    <input type="text" name="name" :value="user.name">
    <button type="submit">Save</button>
  </form>
</serc-rod>

Using :value on OPTION inside SELECT:

<serc-rod id="country-form" data='{
  "countries": [
    { "code": "US", "label": "United States" },
    { "code": "JP", "label": "Japan" }
  ],
  "selected": "JP"
}'>
  <select name="country">
    <option value="">Select a country</option>
    <option *each="c of countries" :value="c.code">
      {{%c.label%}}
    </option>
  </select>
</serc-rod>

In this example, :value ensures that each option’s value attribute matches c.code. The selected option still depends on standard browser behaviour or on other directives for selection state.

Notes