Public
Edited
Apr 25, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" rel="stylesheet">
</head>
Insert cell
Insert cell
buttonGroup = html`
<div style="display: flex;">
<label style="background-color: ${buttonGroup === 'option1' ? 'blue' : 'gray'};">
<input type="radio" name="button-group" value="option1" checked=${buttonGroup === 'option1'} onchange=${() => buttonGroup = 'option1'}>
Option 1
</label>
<label style="background-color: ${buttonGroup === 'option2' ? 'blue' : 'gray'};">
<input type="radio" name="button-group" value="option2" checked=${buttonGroup === 'option2'} onchange=${() => buttonGroup = 'option2'}>
Option 2
</label>
<label style="background-color: ${buttonGroup === 'option3' ? 'blue' : 'gray'};">
<input type="radio" name="button-group" value="option3" checked=${buttonGroup === 'option3'} onchange=${() => buttonGroup = 'option3'}>
Option 3
</label>
</div>
`;
Insert cell
function buttonGroup(options, {value: initialValue}) {
const group = html`<span style="display: flex"></span>`;
let value = initialValue;
function update() {
for (const button of group.children) {
button.style.background = button.value === value ? "#ddd" : null;
}
}
function input(event) {
value = event.target.value;
update();
group.dispatchEvent(new CustomEvent("input"));
}
for (const option of options) {
const button = html`<button style="flex: 1">${option.label}</button>`;
button.value = option.value;
button.onclick = input;
group.appendChild(button);
}
update();
return Object.defineProperty(group, "value", {
get() {
return value;
},
set(newValue) {
value = newValue;
update();
}
});
}
Insert cell
options = [
{label: "Option 1", value: "option1"},
{label: "Option 2", value: "option2"},
{label: "Option 3", value: "option3"}
];
Insert cell
group = buttonGroup(options, {value: "option1"});
Insert cell
group.addEventListener("input", () => {
console.log(`Selected value: ${group.value}`);
});
Insert cell
group.value
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more