# Multiple input elements controlling a reactive variable

**URL:** https://talk.observablehq.com/t/multiple-input-elements-controlling-a-reactive-variable/189
**Category:** Help
**Created:** [February 1, 2018, 8:01pm UTC](https://talk.observablehq.com/t/multiple-input-elements-controlling-a-reactive-variable/189 "2018-02-01T20:01:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![thSoft](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/thsoft/32/123_2.png) [@thSoft](https://talk.observablehq.com/u/thSoft)
#### Post date: [February 1, 2018, 8:01pm UTC](https://talk.observablehq.com/t/multiple-input-elements-controlling-a-reactive-variable/189/1 "2018-02-01T20:01:49Z")

</div>

What is the idiomatic way of having two input elements both showing the value of the same variable and controlling it? E.g. a number that can be set both on a range slider and on a numeric input field, while both are reflecting its value.

I tried so far:

```
viewof number = html`<input type="range">`
html`<input type="number" value="${number}"/>`

```

…but the number input only displays the value, does not control it.

Moreover, it would be great if there was a way to formulate this as a “component”, i.e. reusable in other contexts (maybe as a function?)

Thank you in advance!

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [February 1, 2018, 9:10pm UTC](https://talk.observablehq.com/t/multiple-input-elements-controlling-a-reactive-variable/189/2 "2018-02-01T21:10:18Z")

</div>

Combine the two inputs into a cell, and then use _input_ event listeners to coordinate changes between them. The _input_ events will bubble up so that `viewof` will respond to interaction on either input. You just need to set _element_.value on the element returned by the cell so that the desired value is exposed to other cells in the notebook.

Here’s some code:

```auto
viewof x = {
  const div = html`
    <input type=range min=0 max=100>
    <input type=number min=0 max=100 style="width:auto;">
  `;
  const range = div.querySelector("[type=range]");
  const number = div.querySelector("[type=number]");
  div.value = range.value = number.value = 50;
  range.addEventListener("input", () => number.value = div.value = range.valueAsNumber);
  number.addEventListener("input", () => range.value = div.value = number.valueAsNumber);
  return div;
}

```

And here’s a live demo: [https://beta.observablehq.com/@mbostock/linked-inputs](https://beta.observablehq.com/@mbostock/linked-inputs)

---

<div class="post-metadata">

### Author: ![thSoft](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/thsoft/32/123_2.png) [@thSoft](https://talk.observablehq.com/u/thSoft)
#### Post date: [February 1, 2018, 9:13pm UTC](https://talk.observablehq.com/t/multiple-input-elements-controlling-a-reactive-variable/189/3 "2018-02-01T21:13:17Z")

</div>

Wow, thank you very much for the quick and comprehensive answer!
