Why do viewof cells disappear when referred to in another cell

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

Not super important - just a bit curious.

1 Like

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.

2 Likes

@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 that does it, but it’s been a while since I studied this…)

3 Likes

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):

image

An example of a workaround can be found here:

1 Like