Beginner · About 10 minutes · Basic HTML and JavaScript
You will build a page that greets someone as they type their name. Clearing the input produces a fallback greeting. A reset button puts the original name back into both the input and the message.
This uses the standalone domma-reactive package. You do not need Domma JS, Domma CMS, npm or a build tool to follow along. The library loads from a CDN, so you will need an internet connection.
1. Try the finished page
Type a different name, clear the field, and press Reset.
Notice that the input and message agree after a reset, too. Both read the same state.
2. Save the complete file
Copy this into index.html and open it in a modern browser. The module import pins version 1.3.1 so the example does not change underneath you.
You can also open the complete HTML file and save it from your browser.
<!doctype html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your first reactive page</title>
<style>
body { font: 18px/1.5 system-ui, sans-serif; max-width: 36rem; margin: 3rem auto; padding: 0 1rem; }
input, button { font: inherit; padding: .5rem; }
label { display: grid; gap: .5rem; }
</style>
</head>
<body>
<h1>Your first reactive page</h1>
<main id="app">
<div class="demo-stage">
<label>
Your name
<input class="form-input" data-model="name.value" autocomplete="off">
</label>
<p data-bind-text="greeting.value"></p>
<button type="button" class="btn btn-sm" data-on-click="reset">Reset</button>
</div>
</main>
<script type="module">
import {observable, computed, applyBindings} from 'https://cdn.jsdelivr.net/npm/domma-reactive@1.3.1/dist/domma-reactive.esm.js';
function createGreeting() {
const name = observable('Ada');
const greeting = computed(() => {
const person = name.value.trim();
return person ? `Hello, ${person}!` : 'Hello, stranger!';
});
return {
name,
greeting,
reset() { name.value = 'Ada'; }
};
}
applyBindings(createGreeting(), document.querySelector('#app'));
</script>
</body>
</html>If your browser blocks module imports from a local file, serve the folder with a local web server. With Python installed, run python3 -m http.server 8000 in that folder and visit http://localhost:8000.
3. Give changing data a home
observable('Ada') creates a cell holding the name. JavaScript reads and writes that cell through .value:
const name = observable('Ada');
name.value = 'Grace';
The greeting computed reads name.value. That read establishes its dependency on the name. When the name changes, the derived message can update without a separate change handler.
The model used by the live demo is shown below. The standalone file above uses the same model.
export function createGreeting() {
const name = observable('Ada');
const greeting = computed(() => {
const person = name.value.trim();
return person ? `Hello, ${person}!` : 'Hello, stranger!';
});
return {
name,
greeting,
reset() { name.value = 'Ada'; }
};
}trim() treats an empty input and an input containing only spaces alike. It changes how the greeting is derived; it does not rewrite what someone is typing.
4. Attach the HTML to that data
The live example uses this markup:
<div class="demo-stage">
<label>
Your name
<input class="form-input" data-model="name.value" autocomplete="off">
</label>
<p data-bind-text="greeting.value"></p>
<button type="button" class="btn btn-sm" data-on-click="reset">Reset</button>
</div>There are three bindings:
data-model="name.value"connects the input to the observable's value. Typing writes through that property, and a JavaScript write updates the field.data-bind-text="greeting.value"reads the computed value and puts it into the paragraph as text.data-on-click="reset"calls the model's reset function when you press the button.
Both expressions use .value. The model binding needs a writable path, while the text binding reads the computed value to display.
Finally, applyBindings(model, element) activates the bindings inside your chosen element. Your existing HTML remains the page; the bindings keep the relevant parts in step with the model.
5. Check your understanding
Try these changes in your saved file:
- Change the initial name to your own. Reload and check the input and greeting.
- Change the fallback to
What should we call you?. Clear the field to see it. - Change
reset()to write an empty string. Press Reset and check both elements.
When a binding seems unresponsive, check the browser console first. Then check whether you used .value in both the model path and the text expression.
Build something larger
The contacts tutorial adds keyed lists, validation, filtering, persistence, components and slots. If you want more background first, read Add reactivity to an ordinary HTML page.
