Skip to content

Commit 15bdee3

Browse files
authored
Update html-dom-simplified.mdx
1 parent 6b9d0ef commit 15bdee3

File tree

1 file changed

+105
-3
lines changed

1 file changed

+105
-3
lines changed

docs/technologies/javascript/html-dom-simplified.mdx

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const header = document.querySelector("#heading");
103103
header.textContent = "HTML DOM Manipulation";
104104
```
105105

106-
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".
107107

108108
### Using the `innerHTML` property
109109

@@ -116,7 +116,9 @@ paragraph.innerHTML = "<h3>DOM Manipulation made easy</h3>";
116116

117117
## Adding elements to the HTML DOM in JavaScript
118118

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

121123
You can add a new list item to the `ul` tag on the web page as done below.
122124

@@ -136,7 +138,107 @@ The code snippet above,
136138
- gets the `ul` element from the web page,
137139
- creates a new `li` element, sets its content, and adds a class attribute to the `li` element, then,
138140
- 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.
145+
146+
```js
147+
const list = document.querySelector(".list");
148+
const firstItem = document.querySelector(".item");
149+
150+
console.log(firstItem); //Output: 👉🏻 <li class="item">JavaScript Beginners</li>
151+
list.removeChild(firstItem);
152+
```
153+
154+
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+
<h1 id="heading">HTML DOM Basics</h1>
164+
<p id="paragraph">Learn how to manipulate the DOM with JavaScript</p>
165+
<ul class="list">
166+
<li class="item">JavaScript Beginners</li>
167+
<li class="item">JavaScript Intermediates</li>
168+
<li class="item">JavaScript Experts</li>
169+
</ul>
170+
<button id="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+
const button = 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+
const button = 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+
const button = 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+
const button = 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.
140242

141243
## Resources
142244

0 commit comments

Comments
 (0)