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: 2-ui/1-document/07-modifying-document/article.md
+26-13
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ Here's how it will look:
28
28
*/!*
29
29
```
30
30
31
-
That was an HTML example. Now let's create the same `div` with JavaScript (assuming that the styles are in the HTML or an external CSS file).
31
+
That was the HTML example. Now let's create the same `div` with JavaScript (assuming that the styles are in the HTML/CSS already).
32
32
33
33
## Creating an element
34
34
@@ -48,21 +48,28 @@ To create DOM nodes, there are two methods:
48
48
let textNode = document.createTextNode('Here I am');
49
49
```
50
50
51
+
Most of the time we need to create element nodes, such as the `div` for the message.
52
+
51
53
### Creating the message
52
54
53
-
In our case the message is a `div` with `alert` class and the HTML in it:
55
+
Creating the message div takes 3 steps:
54
56
55
57
```js
58
+
// 1. Create <div> element
56
59
let div =document.createElement('div');
60
+
61
+
// 2. Set its class to "alert"
57
62
div.className="alert";
63
+
64
+
// Fill it with the content
58
65
div.innerHTML="<strong>Hi there!</strong> You've read an important message.";
59
66
```
60
67
61
-
We created the element, but as of now it's only in a variable. We can't see the element on the page, as it's not yet a part of the document.
68
+
We've created the element. But as of now it's only in a variable named `div`, not in the pageyet. So we can't see it.
62
69
63
70
## Insertion methods
64
71
65
-
To make the `div` show up, we need to insert it somewhere into `document`. For instance, in`document.body`.
72
+
To make the `div` show up, we need to insert it somewhere into `document`. For instance, into `<body>` element, referenced by`document.body`.
66
73
67
74
There's a special method `append` for that: `document.body.append(div)`.
68
75
@@ -90,14 +97,20 @@ Here's the full code:
90
97
</script>
91
98
```
92
99
93
-
This set of methods provides more ways to insert:
100
+
Here we called `append` on `document.body`, but we can call `append` method on any other element, to put another element into it. For instance, we can append something to `<div>` by calling `div.append(anotherElement)`.
94
101
95
-
-`node.append(...nodes or strings)` -- append nodes or strings at the end of `node`,
96
-
-`node.prepend(...nodes or strings)` -- insert nodes or strings at the beginning of `node`,
97
-
-`node.before(...nodes or strings)` –- insert nodes or strings before `node`,
98
-
-`node.after(...nodes or strings)` –- insert nodes or strings after `node`,
102
+
Here are more insertion methods, they specify different places where to insert:
103
+
104
+
-`node.append(...nodes or strings)` -- append nodes or strings *at the end* of `node`,
105
+
-`node.prepend(...nodes or strings)` -- insert nodes or strings *at the beginning* of `node`,
106
+
-`node.before(...nodes or strings)` –- insert nodes or strings *before*`node`,
107
+
-`node.after(...nodes or strings)` –- insert nodes or strings *after*`node`,
99
108
-`node.replaceWith(...nodes or strings)` –- replaces `node` with the given nodes or strings.
100
109
110
+
Arguments of these methods are an arbitrary list of DOM nodes to insert, or text strings (that become text nodes automatically).
111
+
112
+
Let's see them in action.
113
+
101
114
Here's an example of using these methods to add items to a list and the text before/after it:
102
115
103
116
```html autorun
@@ -121,7 +134,7 @@ Here's an example of using these methods to add items to a list and the text bef
121
134
</script>
122
135
```
123
136
124
-
Here's a visual picture what methods do:
137
+
Here's a visual picture of what the methods do:
125
138
126
139

127
140
@@ -139,7 +152,7 @@ before
139
152
after
140
153
```
141
154
142
-
These methods can insert multiple lists of nodes and text pieces in a single call.
155
+
As said, these methods can insert multiple nodes and text pieces in a single call.
143
156
144
157
For instance, here a string and an element are inserted:
145
158
@@ -150,7 +163,7 @@ For instance, here a string and an element are inserted:
150
163
</script>
151
164
```
152
165
153
-
All text is inserted *as text*.
166
+
Please note: the text is inserted "as text", not "as HTML", with proper escaping of characters such as `<`, `>`.
154
167
155
168
So the final HTML is:
156
169
@@ -166,7 +179,7 @@ In other words, strings are inserted in a safe way, like `elem.textContent` does
166
179
167
180
So, these methods can only be used to insert DOM nodes or text pieces.
168
181
169
-
But what if we want to insert HTML "as html", with all tags and stuff working, like `elem.innerHTML`?
182
+
But what if we'd like to insert an HTML string "as html", with all tags and stuff working, in the same manner as `elem.innerHTML` does it?
0 commit comments