You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/javascript/display.md
+18-13Lines changed: 18 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,30 +8,35 @@ const x = Math.random();
8
8
display(x);
9
9
```
10
10
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.
12
12
13
-
<!-- TODO This is an obscure, pedagogical technique and not the best initial demonstration of display. -->
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.)
14
18
15
19
```js echo
16
-
constspan=document.createElement("span");
17
-
span.appendChild(document.createTextNode("Your lucky number is "));
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):
24
26
25
27
```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)}!`));
27
29
```
28
30
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
+
<divclass="note">
32
+
<p>This is a contrived example — you wouldn’t normally create a text node by hand. Instead, you’d use an <ahref="../javascript#inline-expressions">inline expression</a> to interpolate a value into Markdown. For example:</p>
33
+
<pre><codeclass="language-md">Your lucky number is ${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 <ahref="../lib/plot">Observable Plot</a> or <ahref="../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:
30
37
31
38
```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)}!`);
35
40
```
36
41
37
42
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