-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
287 lines (287 loc) · 11.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DOM</title>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<header>
<h1>JavaScript</h1>
</header>
<section>
<h2>
Creating HTML Elements using <span>insertAdjacentHTML()</span> method
</h2>
<div class="main-content-container">
<div class="code-content-container">
<div class="method-description-container">
<p>
We will use
<span class="bold-blue-text">insertAdjacentHTML()</span> method to
create HTML elements and add them to the DOM.
</p>
<h3>insertAdjacentHTML() method</h3>
<p>
<span class="green-color-text">element<span class="bold-blue-text">.insertAdjacentHTML(<span class="red-color-text">position</span>,
<span class="red-color-text">stringHtml</span>)</span></span>:
</p>
<p>
The <span class="green-color-text">element</span> is the existing
HTML element, relative to which we are adding the new HTML element
that we create.
</p>
<p>
The <span class="red-color-text">position</span> parameter
indicates where exactly we insert the new created HTML.
</p>
<p>
The <span class="red-color-text">stringHtml</span> paramater is
this new HTML element that we create. The type of
<span class="red-color-text">stringHtml</span> parameter is a
string which contains HTML code.
</p>
<p>
For example, we can use the
<span class="bold-blue-text">insertAdjacentHTML()</span> method to
add new <span class="gray-color-text"><li></span> elements
to the parent <span class="gray-color-text"><ul></span> HTML
element that already contains three existing <li> elements.
</p>
</div>
<div class="ul-container">
<div>
<ul class="ul-existing-htm">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<div>
<p>We create the followig JavaScript code to do it:</p>
<div class="js-code-container">
<p>
const ul = document.querySelector(".ul-insert-html-example");
</p>
<p>
const li = `<li>element added
by<span>JavaScript</span></li>`;
</p>
<p>ul.insertAdjacentHTML("afterbegin", li);</p>
</div>
<p>And we have the result:</p>
</div>
<div class="ul-container">
<div>
<ul class="ul-insert-html-example">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<p>
We can use the following parameters to insert the created HTML
element:
</p>
<p>
<span class="gray-color-text">"afterbegin"</span>,
<span class="gray-color-text">"beforeend"</span>,
<span class="gray-color-text">"beforebegin"</span>,
<span class="gray-color-text">"afterend"</span>.
</p>
<p>
<span class="gray-color-text">"afterbegin"</span> - "responses" for
inserting created HTML element before the first child element of
theparent element:
</p>
<div class="ul-container">
<div>
<ul class="ul-afterbegin">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<p>
<span class="gray-color-text">"beforeend"</span> - "responses" for
inserting created HTML element after the last child element of the
parent element:
</p>
<div class="ul-container">
<div>
<ul class="ul-beforeend">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<p>
<span class="gray-color-text">"beforebegin"</span> - "responses" for
inserting created HTML element before its parent element:
</p>
<div class="ul-container">
<div>
<ul class="ul-beforebegin">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<div>
<p>
<span class="gray-color-text">"afterend"</span> -"responses" for
inserting created HTML element after its parent element:
</p>
<div class="ul-container">
<div>
<ul class="ul-afterend">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<p>
We can easily use the
<span class="bold-blue-text">insertAdjacentHTML()</span> method
for creating and
<a
href="https://romannet77.github.io/js-event-delegation/"
target="_blank"
>adding HTML elements dynamically by event
</a>!
</p>
<p>
For example, we want to add the new
<span class="gray-color-text"><li></span> element to the
parent <span class="gray-color-text"><ul></span> element by
the click
<span class="gray-color-text"><button></span> event.
</p>
<p>
First of all, let's create a button "Add <li> element" using
the insertAdjacentHTML() method !
</p>
<div class="js-code-container">
<p>
const buttonHtmlElement = `<button
class="button-add-html-element"> Add <li>
element</button>`;
</p>
<p>
const ulParentEl = document.querySelector(
"ul.parent-element-for-button" );
</p>
<p>
ulParentEl.insertAdjacentHTML( "beforebegin",
buttonHtmlElement);
</p>
</div>
<div class="ul-container">
<div>
<ul class="parent-element-for-button">
<li>existing <li> html element</li>
<li>existing <li> html element</li>
<li>existing <li> html element</li>
</ul>
</div>
</div>
<div>
<p>
Now we can attach an event handler to the created
<span class="gray-color-text"><button></span>:
</p>
<div class="js-code-container">
<p>const button = document.querySelector("button");</p>
<p>
const liAddedByClick = `<li>added by click<span>
class="red-color-text" (JavaScript)</span></li>`;
</p>
<div class="js-function">
<p>button.addEventListener("click", () => {</p>
<div class="function-body-part">
<p>
ulParentEl.insertAdjacentHTML( "afterbegin",
liAddedByClick);
</p>
</div>
<p> }</p>
<p>);</p>
</div>
</div>
<p>
So we can add
<span class="gray-color-text"><li></span> elements by
clicking created button !
</p>
<p>
<span class="bold-blue-text"
>Try It! Click the "Add <li> element" button !</span>
</p>
<div class="conclusion-container">
<h3>Conclusion</h3>
<p>
All that we need to create and add the HTML element to the
DOM, using the JavaScript<span class="bold-blue-text">
insertAdjacentHTML() </span>method, is:
</p>
<p>
1)<span class="conclusion-text">
Create the HTML element that we need:</span><br />
const li = `<li>element added by
<span>JavaScript</span></li>`;
</p>
<p>
2)<span class="conclusion-text">
Choose its parent HTML element:</span><br />
const ul = document.querySelector(".ul-insert-html-example");
</p>
<p>
3)<span class="conclusion-text">
Indicate the place where we want to store the created HTML
element:</span><br />
ul.insertAdjacentHTML("afterbegin", li);
</p>
<p>
And that's it! And that is the simplicity and the beauty of
the
<span class="bold-blue-text">insertAdjacentHTML()</span>
method !
</p>
</div>
</div>
</div>
</div>
</div>
</section>
<footer>
<div class="footer-img">
<a href="https://github.com/Romannet77" target="_blank">
<img class="my-photo" src="./images/my-photo.jpeg" alt="my-photo" />
</a>
</div>
<div class="profile-links">
<a
href="https://www.linkedin.com/in/roman-yurchenko-89630923b/"
target="_blank">
<img src="./images/linkedin-icon.svg" alt="LinkedIn-img" />
</a>
<a href="https://github.com/Romannet77" target="_blank">
<img src="./images/github-icon.svg" alt="Git-img" />
</a>
</div>
<p class="author-info">
© 2022 Written and Developed by Mr.
<span><a href="https://github.com/Romannet77" target="_blank">Roman Yurchenko</a></span>
</p>
</footer>
<script src="./script.js"></script>
</body>
</html>