# The \`.value\` is a lie.🎂 (on viewof'ed inputs at least)

**URL:** https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343
**Category:** Help
**Created:** [December 16, 2020, 12:42pm UTC](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343 "2020-12-16T12:42:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![somethingelseentire](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/somethingelseentire/32/3912_2.png) [@somethingelseentire](https://talk.observablehq.com/u/somethingelseentire)
#### Post date: [December 16, 2020, 12:42pm UTC](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343/1 "2020-12-16T12:42:26Z")

</div>

Hey folks,  
According to [https://observablehq.com/@observablehq/introduction-to-views](https://observablehq.com/@observablehq/introduction-to-views)  
these two snippets should be equivalent.

```auto
box // true

```

```auto
(box, viewof box.value) // "on"

```

given that

```auto
viewof box = html`<input type="checkbox" checked>`

```

However the latter returns the `value` attribute of the dom node, which is “on” by default.

So what kind of magic is actually happening under the hood?  
Is Observable just treating inputs differently and using `value` for everything else?

Resetting the value and firing an `input` event doesn’t actually change the value propagated to the cells depending on it, probably because it’s fired on the input cell, and not on the stdlib `Generators.input` object.

Is it possible to access that object directly? Should `Generators.input` register an `input` event listener on it’s wrapped value? Thoughts?

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [December 16, 2020, 1:31pm UTC](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343/2 "2020-12-16T13:31:56Z")

</div>

Think of `viewof` as a macro that produces an additional Runtime `Variable` (internal represenation of a cell) for your viewof cell, so that you end up with two Variables:

- `viewof box`, the cell that you originally defined,
- `box`, which is a generator cell that listens to changes in `viewof box` via Generators.input and yields the new value.

Generators.input has some “magic” for inputs. Depending on the input type, it will:

- listen to different events (e.g. “click” for checkboxes),
- cast the value (e.g. via `.valueAsNumber()` for number and range inputs).

You can find the implementation details here:

> <https://github.com/observablehq/stdlib/blob/master/src/generators/input.js>

In general I find that looking at a notebook’s generated source can give some insight into the underlying logic, e.g.:  
[https://api.observablehq.com/@observablehq/introduction-to-views.js?v=3](https://api.observablehq.com/@observablehq/introduction-to-views.js?v=3)

---

<div class="post-metadata">

### Author: ![somethingelseentire](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/somethingelseentire/32/3912_2.png) [@somethingelseentire](https://talk.observablehq.com/u/somethingelseentire)
#### Post date: [December 16, 2020, 2:18pm UTC](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343/3 "2020-12-16T14:18:49Z")

</div>

Hey @mootari,  
Yeah, so the `.value` is just the default case.  
However then I’d actually argue that `Generators.input` has a bug, in that it should also listen for “input” events.  
Yes one could emit a ‘clicked’ event for checkboxes e.t.c. but that feels like a weird API violation, no? I mean ideally instead of using ‘value’ observable would probably use a `Symbol('value')`  
to prevent data transparency issues, but I guess that’s too late now 😃.

Btw, while I got you on the figurative phone, there’s a small bug in [Mutable Forms / Fabian Iwand / Observable](https://observablehq.com/@mootari/mutable-forms) where it breaks for `checkbox` with just a single value. I haven’t tracked down the bug far enough, but it looks like browsers automatically wrap multiple checkboxes in a `RadioNodeList` but leave single checkboxes as is.  
Yay auto unwrapping -.- .

---

<div class="post-metadata">

### Author: ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)
#### Post date: [December 16, 2020, 3:08pm UTC](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343/4 "2020-12-16T15:08:04Z")

</div>

> [@somethingelseentire](#):
>
> However then I’d actually argue that `Generators.input` has a bug, in that it should also listen for “input” events.

That would cause the generator to yield multiple times for a single update. If you want to investigate and visualize the events, have a look at [Monitoring Events / Fabian Iwand | Observable](https://observablehq.com/@mootari/monitoring-events).

> [@somethingelseentire](#):
>
> However then I’d actually argue that `Generators.input` has a bug, in that it should also listen for “input” events

As far as I recall there are some problems with input events on checkbox elements, specifically that the checked state will still have the old state when the event is raised. However, you can work around the issue by wrapping your element in a span or div and either have the input event bubble, or trigger it directly on the wrapper.

> [@somethingelseentire](#):
>
> there’s a small bug in [Mutable Forms / Fabian Iwand | Observable](https://observablehq.com/@mootari/mutable-forms) where it breaks for `checkbox` with just a single value

Thanks, [fixed](https://observablehq.com/compare/6b56a2a00cbb4676@560...6b56a2a00cbb4676@605)!

---

<div class="post-metadata">

### Author: ![somethingelseentire](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/somethingelseentire/32/3912_2.png) [@somethingelseentire](https://talk.observablehq.com/u/somethingelseentire)
#### Post date: [December 16, 2020, 3:44pm UTC](https://talk.observablehq.com/t/the-value-is-a-lie-on-viewofed-inputs-at-least/4343/5 "2020-12-16T15:44:42Z")

</div>

Ah yeah you’re right.  
Normally I really appreciate observables “nothing in-between” approach, but I think in this case it would have been fine to add a bunch of shim methods to input elements the moment they are passed to `Observers.input`, which abstract away the different behaviours and package them into one neat “input” event and `Symbol(value)` attribute.

Anyways, thanks for the help, and the fix! 😃
