Published
Edited
Apr 26, 2021
1 star
Insert cell
Insert cell
viewof playing1 = {
let playing = false;

const node = html`<div><button>Play</button></div>`;
const button = node.querySelector("button");

button.addEventListener("click", function (e) {
node.value = playing = !playing;
node.dispatchEvent(new CustomEvent("input"));

if (playing) {
button.textContent = "Pause";
} else {
button.textContent = "Play";
}
});

node.value = playing;
return node;
}
Insert cell
playing1
Insert cell
Insert cell
viewof playing2 = {
let value = false;

const button = html`<button onclick=${() => set(node, !value)}>`;
const node = html`<div>${button}</div>`;

// Update the display whenever the value changes
Object.defineProperty(node, "value", {
get() {
return value;
},
set(v) {
value = v;
button.textContent = v ? "Pause" : "Play";
}
});

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

return node;
}
Insert cell
playing2
Insert cell
Insert cell
html`<button onclick=${() => (mutable playing3 = !mutable playing3)}>
${playing3 ? "Pause" : "Play"}`
Insert cell
mutable playing3 = false
Insert cell
Insert cell
Insert cell
import { html } from "@observablehq/inputs"
Insert cell
import { set } from "@observablehq/synchronized-inputs"
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