# Why do viewof cells disappear when referred to in another cell

**URL:** <https://talk.observablehq.com/t/why-do-viewof-cells-disappear-when-referred-to-in-another-cell/4159>\
**Category:** Help\
**Created:** [November 16, 2020, 11:48pm UTC](https://talk.observablehq.com/t/why-do-viewof-cells-disappear-when-referred-to-in-another-cell/4159 "2020-11-16T23:48:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)\
**Post date:** [November 16, 2020, 11:48pm UTC](https://talk.observablehq.com/t/why-do-viewof-cells-disappear-when-referred-to-in-another-cell/4159/1 "2020-11-16T23:48:28Z")

</div>

The title states the question pretty completely: Why do viewof cells disappear when referred to in another cell? Here’s an example:

> **[Viewof in other cells](https://observablehq.com/d/dee8471f0e534813)**
>
> Viewof in other cells The cell named both just below refers to two viewof cells defined below that. I wonder why those viewof cells disappear once they're referred to by another cell?

Not super important - just a bit curious.

---

<div class="post-metadata">

**Author:** ![aaronkyle](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/aaronkyle/32/8106_2.png) [@aaronkyle](https://talk.observablehq.com/u/aaronkyle)\
**Post date:** [November 16, 2020, 11:57pm UTC](https://talk.observablehq.com/t/why-do-viewof-cells-disappear-when-referred-to-in-another-cell/4159/2 "2020-11-16T23:57:16Z")

</div>

Fun question! I don’t have the technical answer for you (clearly, I am such a non-technical person), but my understanding of this is b/c the first `viewof` “constructs” the elements and the second `view of` “utilizes” it. The element only exists once in the DOM, so it gets jerked around according to the last function to utilize.

---

<div class="post-metadata">

**Author:** ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)\
**Post date:** [November 17, 2020, 12:38am UTC](https://talk.observablehq.com/t/why-do-viewof-cells-disappear-when-referred-to-in-another-cell/4159/3 "2020-11-17T00:38:53Z")

</div>

@aaronkyle is exactly right. Your two `viewof` cells have DOM elements as their output values, and the `html` function in the `both` cell grabs those elements from their original locations on the page and includes it in its output. (I think it’s [this line of code](https://github.com/observablehq/stdlib/blob/master/src/template.js#L61) that does it, but it’s been a while since I studied this…)

---

<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:** [November 17, 2020, 10:22am UTC](https://talk.observablehq.com/t/why-do-viewof-cells-disappear-when-referred-to-in-another-cell/4159/4 "2020-11-17T10:22:29Z")

</div>

Note that Observable’s Inspector attempts to mitigate this to some degree. If the returned element already has a parent, it won’t return the actual DOM node ([source](https://github.com/observablehq/inspector/blob/ed6eb87359596e4d25ae75b22598c7a2cd3e585e/src/index.js#L20)):

![image](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/6/6ca69c2b9f785308a9ff4728b0ff6b3a4cb2f628.png)

An example of a workaround can be found here:

> **[Dashboard with Inputs](https://observablehq.com/@mootari/dashboard-with-inputs)**
>
> Dashboard with Inputs Context / discussion: Redefine a value for viewof ? The function below receives an HTML element and returns it as a view to which other elements can be added from outside the cell.
