More ui-bind commands: attributes, values, state and formatting
Besides text, show and hide, ui-bind has commands for the common DOM changes. Each command is name:expression (or name selector:expression to apply it to an inner element), and value is the value of the path:
| Command | Does |
|---|---|
html:expr | sets the inner HTML (escape it yourself!) |
title:expr, href:expr, src:expr | sets that attribute |
attr name:expr | sets any attribute |
value:expr | sets the value of a form control |
disabled:expr / enabled:expr | disables or enables the controls inside |
checked:expr | checks the checkbox inside |
.class:expr | adds or removes the class |
Options of the whole element: format:yyyy-MM-dd formats a date value before it is printed (for numbers call format() in the expression: text:value.format(2)), empty:none is shown when the value is empty, changes skips updates when the value did not change, and delay:200 waits that many milliseconds before rendering.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jComponent</title>
<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
<ui-bind path="user" config="attr data-id:value.id;title:value.name;text:value.name" class="greeting"></ui-bind>
<ui-bind path="product.price" config="text:value.format(2)" class="price"></ui-bind>
<ui-bind path="product.date" config="text:value;format:yyyy-MM-dd" class="date"></ui-bind>
<ui-bind path="user.admin" config="disabled:!value"><button type="button" id="delete">Delete</button></ui-bind>
<ui-bind path="user.nick" config="value input:value;empty:anonymous"><input type="text" /></ui-bind>
<ui-bind path="user.nick" config="text:value;empty:anonymous" class="nick"></ui-bind>
<script>
SET('user', { id: 7, name: 'Peter', admin: false, nick: '' });
SET('product', { price: 1234.5, date: new Date(2026, 9, 16) });
</script>
</body>
</html> Try it in your browser
Edit the example and run it. It runs in an isolated frame in your browser; nothing is sent to our server.
Preview