Skip to content

Commit 340ce43

Browse files
committed
minor fixes
1 parent b3fbb9c commit 340ce43

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

2-ui/1-document/07-modifying-document/article.md

+26-13
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Here's how it will look:
2828
*/!*
2929
```
3030

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).
3232

3333
## Creating an element
3434

@@ -48,21 +48,28 @@ To create DOM nodes, there are two methods:
4848
let textNode = document.createTextNode('Here I am');
4949
```
5050

51+
Most of the time we need to create element nodes, such as the `div` for the message.
52+
5153
### Creating the message
5254

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

5557
```js
58+
// 1. Create <div> element
5659
let div = document.createElement('div');
60+
61+
// 2. Set its class to "alert"
5762
div.className = "alert";
63+
64+
// Fill it with the content
5865
div.innerHTML = "<strong>Hi there!</strong> You've read an important message.";
5966
```
6067

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 page yet. So we can't see it.
6269

6370
## Insertion methods
6471

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`.
6673

6774
There's a special method `append` for that: `document.body.append(div)`.
6875

@@ -90,14 +97,20 @@ Here's the full code:
9097
</script>
9198
```
9299

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)`.
94101

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`,
99108
- `node.replaceWith(...nodes or strings)` –- replaces `node` with the given nodes or strings.
100109

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+
101114
Here's an example of using these methods to add items to a list and the text before/after it:
102115

103116
```html autorun
@@ -121,7 +134,7 @@ Here's an example of using these methods to add items to a list and the text bef
121134
</script>
122135
```
123136

124-
Here's a visual picture what methods do:
137+
Here's a visual picture of what the methods do:
125138

126139
![](before-prepend-append-after.svg)
127140

@@ -139,7 +152,7 @@ before
139152
after
140153
```
141154

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.
143156

144157
For instance, here a string and an element are inserted:
145158

@@ -150,7 +163,7 @@ For instance, here a string and an element are inserted:
150163
</script>
151164
```
152165

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 `<`, `>`.
154167

155168
So the final HTML is:
156169

@@ -166,7 +179,7 @@ In other words, strings are inserted in a safe way, like `elem.textContent` does
166179

167180
So, these methods can only be used to insert DOM nodes or text pieces.
168181

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?
170183

171184
## insertAdjacentHTML/Text/Element
172185

0 commit comments

Comments
 (0)