Skip to content

Commit 42818cf

Browse files
committed
display edits
1 parent 2124466 commit 42818cf

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

docs/javascript/display.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,35 @@ const x = Math.random();
88
display(x);
99
```
1010

11-
If you pass `display` a DOM element or node, it will be inserted directly into the page. Use this technique to render dynamic displays of data, such as charts and tables.
11+
You can display structured objects, too. Click on the object below to inspect it.
1212

13-
<!-- TODO This is an obscure, pedagogical technique and not the best initial demonstration of display. -->
13+
```js echo
14+
display({hello: {subject: "world"}, numbers: [1, 2, 3, 4]})
15+
```
16+
17+
Calling `display` multiple times will display multiple values. Values are displayed in the order they are received. (Previously-displayed values will be cleared when the associated code block or inline expression is re-run.)
1418

1519
```js echo
16-
const span = document.createElement("span");
17-
span.appendChild(document.createTextNode("Your lucky number is "));
18-
span.appendChild(document.createTextNode(Math.floor(Math.random () * 10)));
19-
span.appendChild(document.createTextNode("!"));
20-
display(span);
20+
for (let i = 0; i < 5; ++i) {
21+
display(i);
22+
}
2123
```
2224

23-
You can create DOM elements using the standard [DOM API](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) or a helper library of your choosing. For example, the above can be written using [Hypertext Literal](../lib/htl) as:
25+
If you pass `display` a DOM node, it will be inserted directly into the page. Use this technique to render dynamic displays of data, such as charts and tables. Here is an example displaying a [text node](https://developer.mozilla.org/en-US/docs/Web/API/Document/createTextNode) created using the [DOM API](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction):
2426

2527
```js echo
26-
display(html`Your lucky number is ${Math.floor(Math.random () * 10)}!`);
28+
display(document.createTextNode(`Your lucky number is ${Math.floor(Math.random () * 10)}!`));
2729
```
2830

29-
You can call `display` multiple times to display multiple values. Values are displayed in the order they are received. Previously-displayed values will be cleared when the associated code block or inline expression is re-run.
31+
<div class="note">
32+
<p>This is a contrived example — you wouldn’t normally create a text node by hand. Instead, you’d use an <a href="../javascript#inline-expressions">inline expression</a> to interpolate a value into Markdown. For example:</p>
33+
<pre><code class="language-md">Your lucky number is &dollar;{Math.floor(Math.random () * 10)}!</code></pre>
34+
</div>
35+
36+
You’ll often pass <code>display</code> a DOM node when you’re using a helper library such as <a href="../lib/plot">Observable Plot</a> or <a href="../lib/inputs">Observable Inputs</a> or a custom component (a function you’ve written that returns a DOM node) to create content. For example, the above can be written using [Hypertext Literal](../lib/htl) as:
3037

3138
```js echo
32-
for (let i = 0; i < 5; ++i) {
33-
display(i);
34-
}
39+
display(html`Your lucky number is ${Math.floor(Math.random () * 10)}!`);
3540
```
3641

3742
The `display` function returns the passed-in value. You can display any value (any expression) in code, not only top-level variables; use this as an alternative to `console.log` to debug your code.

0 commit comments

Comments
 (0)