Published
Edited
Aug 7, 2021
1 fork
Importers
32 stars
Insert cell
Insert cell
function set(input, value) {
input.value = value;
input.dispatchEvent(new Event("input", { bubbles: true })); // Native events bubble, so we should too
}
Insert cell
Insert cell
MyInput = ({value = 0} = {}) => {

const output = htl.html`<output>`;

const node = htl.html`<div>
<button onclick=${() => set(node, value - 1)}>−</button>
<button onclick=${() => set(node, value + 1)}>+</button>
My value is ${output}
</div>`;

// Update the display whenever the value changes
Object.defineProperty(node, "value", {
get() {
return value;
},
set(v) {
output.value = value = +v;
}
});

// Set the initial value
node.value = value;

return node;
}
Insert cell
Insert cell
viewof example = MyInput()
Insert cell
Insert cell
example
Insert cell
Insert cell
Inputs.bind(Inputs.range([-10, 10], {step: 1}), viewof example)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more