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
The code snippet above changes the content of the `h1` tag to `HTML DOM Manipulation`.
106
+
The code snippet above changes the content of the `h1` tag to "HTML DOM Manipulation".
107
107
108
108
### Using the `innerHTML` property
109
109
@@ -116,7 +116,9 @@ paragraph.innerHTML = "<h3>DOM Manipulation made easy</h3>";
116
116
117
117
## Adding elements to the HTML DOM in JavaScript
118
118
119
-
To add new elements to the HTML DOM via JavaScript, you can use the `createElement()` method and the `appendChild()` method. The `createElement()` method creates a new element with JavaScript, and the `appendChild()` adds the newly created element to a parent element on the HTML DOM.
119
+
To add new elements to the HTML DOM via JavaScript, you can use the `createElement()` method and the `appendChild()` method.
120
+
121
+
The `createElement()` method creates a new element with JavaScript, and the `appendChild()` adds the newly created element to a parent element on the HTML DOM.
120
122
121
123
You can add a new list item to the `ul` tag on the web page as done below.
122
124
@@ -136,7 +138,107 @@ The code snippet above,
136
138
- gets the `ul` element from the web page,
137
139
- creates a new `li` element, sets its content, and adds a class attribute to the `li` element, then,
138
140
- appends the `li` element to the `ul` tag on the web page.
139
-
<br />
141
+
142
+
## Removing elements from the HTML DOM in JavaScript
143
+
144
+
To remove elements from the HTML DOM in JavaScript, you can use the `removeChild()` method. This method allows you to remove a child element from its parent element.
The code snippet above gets the entire unordered list and its first child from the web page and removes the first child from the list using the `removeChild()` method.
155
+
156
+
## Listening to HTML DOM events with JavaScript
157
+
158
+
The HTML DOM also allows you to create event listeners that listen to actions performed on a particular element on the web page. You can listen to various events carried out by users on a webpage.
159
+
160
+
Add a button element to the web page. We'll add event listeners to the button.
161
+
162
+
```html
163
+
<h1id="heading">HTML DOM Basics</h1>
164
+
<pid="paragraph">Learn how to manipulate the DOM with JavaScript</p>
165
+
<ulclass="list">
166
+
<liclass="item">JavaScript Beginners</li>
167
+
<liclass="item">JavaScript Intermediates</li>
168
+
<liclass="item">JavaScript Experts</li>
169
+
</ul>
170
+
<buttonid="button">Hello Reader</button>
171
+
```
172
+
173
+
> 💡 PS: Every element has its set of events. You can add event listeners to several HTML elements, including buttons, links, input fields, and more.
174
+
175
+
### Using the `addEventListener()` method
176
+
177
+
The `addEventListener()` method is a built-in method in JavaScript that allows you to add event listeners to HTML elements. It takes two arguments: the type of event to listen for (such as click, mouseover, keydown, etc.) and the function to execute when the event occurs.
178
+
179
+
```js
180
+
constbutton=document.getElementById("button");
181
+
182
+
button.addEventListener("click", function () {
183
+
alert("Button Clicked!");
184
+
});
185
+
```
186
+
187
+
The code snippet above references the button element and listens for a click event on the button. Once a user clicks the button, it triggers the `alert` method.
188
+
189
+
### Using the `element.event` method
190
+
191
+
It is an older way of adding event listeners to HTML elements in JavaScript. It assigns a function to an HTML element.
192
+
193
+
```js
194
+
constbutton=document.getElementById("button");
195
+
196
+
button.onclick=function () {
197
+
alert("Button Clicked!");
198
+
};
199
+
```
200
+
201
+
The `button.onclick` method may look straightforward compared to the `addEventListener` method, but it has some limitations.
202
+
203
+
### `addEventListener()` vs `element.event`
204
+
205
+
- The `addEventListener` method can execute multiple functions when a particular event occurs; this is beneficial when building complex or interactive web applications. In `element.event`, only a single function can be attached to an event.
206
+
207
+
```js
208
+
constbutton=document.getElementById("button");
209
+
210
+
button.addEventListener("click", function () {
211
+
console.log("Button Clicked!");
212
+
});
213
+
214
+
button.addEventListener("click", function () {
215
+
console.log("JavaScript is easy!");
216
+
});
217
+
//👉🏻 Both functions are executed
218
+
```
219
+
220
+
From the code snippet above, both functions are executed when a user clicks the button. Below is an example using the `element.event` method; the last event overrides the previous one.
221
+
222
+
```js
223
+
constbutton=document.getElementById("button");
224
+
225
+
button.onclick=function () {
226
+
console.log("First click!");
227
+
};
228
+
229
+
button.onclick=function () {
230
+
console.log("JavaScript is easy");
231
+
};
232
+
//👉🏻 only the last event is executed
233
+
```
234
+
235
+
- The `addEventListener()` method provides more control over the execution of the event handlers. You can remove an event using the [`removeEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) method.
236
+
237
+
## Conclusion
238
+
239
+
So far, you’ve learnt the basics of what the DOM is and how it works, how to modify elements, add new ones, and listen to events on your web page.
240
+
241
+
A solid understanding of the HTML DOM is essential when building interactive web pages and creating dynamic user interfaces with HTML and JavaScript.
0 commit comments