Skip to content

Commit de86a67

Browse files
committed
HTML DOM Simplified - UPD
1 parent 9f42c42 commit de86a67

File tree

1 file changed

+75
-37
lines changed

1 file changed

+75
-37
lines changed

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

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import SubHeading from "@site/src/components/SubHeading";
99
JavaScript HTML DOM Simplified, Beginner’s Guide
1010
</SubHeading>
1111

12-
The Document Object Model (DOM) is a tree-like structure containing all the elements and content of a web page. It enables you to manipulate the content of a webpage in real-time using JavaScript.
12+
The **Document Object Model** (DOM) is a tree-like structure containing all the elements and content of a web page. It enables you to manipulate the content of a webpage in real-time using **JavaScript**.
1313
With DOM, you can create interactive and dynamic user interfaces.
1414

1515
In this article, you’ll learn how to create event listeners and select, create, modify, and delete elements and attributes on a web page.
1616

17-
## Accessing HTML DOM elements in JavaScript
17+
<br />
18+
19+
## Accessing DOM elements in `JavaScript`
1820

1921
The HTML DOM enables you to access every element on a web page with JavaScript, and there are five ways by which you can access these elements.
2022
Before going over them, create a simple web page as shown below.
@@ -35,66 +37,83 @@ Before going over them, create a simple web page as shown below.
3537
</ul>
3638
```
3739

38-
### Using `document.getElementById()`
40+
<br />
41+
42+
### 👉 Using `getElementById()`
3943

40-
This method allows you to access an HTML element via its id attribute. It returns an entire element or null; if the element does not exist.
44+
This method allows you to `access` an HTML element via its `ID Attribute`. It returns an entire element or null, if the element does not exist.
4145
The code snippet below selects the HTML element with an id of heading from the web page.
4246

4347
```js
4448
const heading = document.getElementById("heading");
45-
console.log(heading); //👉🏻 Output: <h1 id="heading">HTML DOM Basics</h1>
49+
console.log(heading); // 👉🏻 Output: <h1 id="heading">HTML DOM Basics</h1>
4650
```
4751

48-
### Using `document.getElementsByClassName()`
52+
<br />
53+
54+
### 👉 Using `getElementsByClassName()`
4955

50-
With `document.getElementsByClassName()`, you can access elements using their class attribute. It returns an array-like object called an `HTMLCollection` that contains all the elements with the specified class name.
56+
With `document.getElementsByClassName()`, you can access elements using their `Class Attribute`.
57+
It returns an array-like object called an `HTMLCollection` that contains all the elements with the specified class name.
5158

5259
```js
5360
const item = document.getElementsByClassName("item");
54-
console.log(item); //👉🏻 Output: HTMLCollection(3)
61+
console.log(item); // 👉🏻 Output: HTMLCollection(3)
5562
```
5663

57-
### Using `document.getElementsByTagName()`
64+
<br />
5865

59-
This method allows you to access HTML elements via their tag name. It returns an `HTMLCollection` containing all the elements with the same tag name.
66+
### 👉 Using `getElementsByTagName()`
67+
68+
This method allows you to `access` HTML elements via their `Tag Name`. It returns an `HTMLCollection` containing all the elements with the same tag name.
6069

6170
```js
6271
const paragraph = document.getElementsByTagName("p");
63-
console.log(paragraph); //👉🏻 Output: HTMLCollection(1)
72+
console.log(paragraph); // 👉🏻 Output: HTMLCollection(1)
6473
```
6574

6675
The `paragraph` variable contains an array of all the paragraph tags on the web page.
6776

68-
### Using `document.querySelector()`
77+
<br />
78+
79+
### 👉 Using `querySelector()`
6980

70-
With this method, you can access an element via its CSS selector. It returns the first element that matches the selector.
81+
With this method, you can access an element via its `CSS Selector`. It returns the first element that matches the selector.
7182

7283
```js
7384
const paragraph = document.querySelector("#paragraph");
74-
console.log(subHeading); //👉🏻 Output: <p id="paragraph">Learn how to manipulate the DOM with JavaScript</p>
85+
console.log(subHeading); // 👉🏻 Output: <p id="paragraph">Learn how to manipulate the DOM with JavaScript</p>
7586

7687
const list = document.querySelector(".list");
77-
console.log(list); //👉🏻 Output: <ul class="list">...</ul>
88+
console.log(list); // 👉🏻 Output: <ul class="list">...</ul>
7889
```
7990

80-
The code snippet above shows you can reference an element via its ID or class attributes.
91+
The above code snippet shows you can reference an element via its `ID` or `class attributes`.
8192

82-
### Using `document.querySelectorAll()`
93+
<br />
8394

84-
It is similar to the `document.querySelector` method but allows you to access multiple elements using a CSS selector. It returns a `NodeList` of all the HTML elements that match the selector.
95+
### Using `querySelectorAll()`
96+
97+
It is similar to the `document.querySelector` method but allows you to access multiple elements using a CSS selector.
98+
It returns a `NodeList` of all the HTML elements that match the selector.
8599

86100
```js
87101
const items = document.querySelectorAll(".item");
88-
console.log(items); //👉🏻 Output: NodeList(3)
102+
console.log(items); // 👉🏻 Output: NodeList(3)
89103
```
90104

91105
The code snippet above returns all the HTML elements with a class attribute of `item`.
92106

93-
## Modifying HTML DOM elements
107+
<br />
108+
109+
## `Modifying` DOM Elements
94110

95-
One of the most powerful features of JavaScript is its ability to alter HTML elements on a web page in real-time without reloading the entire page. Here, you'll learn how to modify HTML elements with JavaScript.
111+
One of the most powerful features of JavaScript is its ability to alter HTML elements on a web page in real-time without reloading the entire page.
112+
Here, you'll learn how to modify HTML elements with `JavaScript`.
96113

97-
### Using the `textContent` property
114+
<br />
115+
116+
### 👉 Using the `textContent` property
98117

99118
The `textContent` property allows you to change only the content of an HTML element.
100119

@@ -105,7 +124,9 @@ header.textContent = "HTML DOM Manipulation";
105124

106125
The code snippet above changes the content of the `h1` tag to "HTML DOM Manipulation".
107126

108-
### Using the `innerHTML` property
127+
<br />
128+
129+
### 👉 Using the `innerHTML` property
109130

110131
It allows you to modify the content, tags, and attributes of an HTML element. You change the `p` tag to a `h3` tag, as shown below.
111132

@@ -114,7 +135,9 @@ const paragraph = document.getElementById("paragraph");
114135
paragraph.innerHTML = "<h3>DOM Manipulation made easy</h3>";
115136
```
116137

117-
## Adding elements to the HTML DOM in JavaScript
138+
<br />
139+
140+
## `Adding Elements` to the HTML DOM
118141

119142
To add new elements to the HTML DOM via JavaScript, you can use the `createElement()` method and the `appendChild()` method.
120143

@@ -130,7 +153,7 @@ item.textContent = "Newly added";
130153
item.className = "item";
131154

132155
list.appendChild(item);
133-
console.log(item); //👉🏻 Output: <li class="item">Newly added</li>
156+
console.log(item); // 👉🏻 Output: <li class="item">Newly added</li>
134157
```
135158

136159
The code snippet above,
@@ -139,9 +162,12 @@ The code snippet above,
139162
- creates a new `li` element, sets its content, and adds a class attribute to the `li` element, then,
140163
- appends the `li` element to the `ul` tag on the web page.
141164

142-
## Removing elements from the HTML DOM in JavaScript
165+
<br />
166+
167+
## `Removing Elements` from the HTML DOM
143168

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.
169+
To remove elements from the HTML DOM in JavaScript, you can use the `removeChild()` method.
170+
This method allows you to remove a child element from its parent element.
145171

146172
```js
147173
const list = document.querySelector(".list");
@@ -153,9 +179,12 @@ list.removeChild(firstItem);
153179

154180
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.
155181

156-
## Listening to HTML DOM events with JavaScript
182+
<br />
157183

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.
184+
## `Listening` to DOM events with JavaScript
185+
186+
The HTML DOM also allows you to create event listeners that listen to actions performed on a particular element on the web page.
187+
You can listen to various events carried out by users on a webpage.
159188

160189
Add a button element to the web page. We'll add event listeners to the button.
161190

@@ -170,9 +199,11 @@ Add a button element to the web page. We'll add event listeners to the button.
170199
<button id="button">Hello Reader</button>
171200
```
172201

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.
202+
> 💡 Note: Every element has its set of events. You can add event listeners to several HTML elements, including buttons, links, input fields, and more.
203+
204+
<br />
174205

175-
### Using the `addEventListener()` method
206+
### 👉 Using the `addEventListener()` method
176207

177208
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.
178209

@@ -186,7 +217,9 @@ button.addEventListener("click", function () {
186217

187218
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.
188219

189-
### Using the `element.event` method
220+
<br />
221+
222+
### 👉 Using the `element.event` method
190223

191224
It is an older way of adding event listeners to HTML elements in JavaScript. It assigns a function to an HTML element.
192225

@@ -200,9 +233,12 @@ button.onclick = function () {
200233

201234
The `button.onclick` method may look straightforward compared to the `addEventListener` method, but it has some limitations.
202235

203-
### `addEventListener()` vs `element.event`
236+
<br />
237+
238+
### 👉 `addEventListener()` vs `element.event`
204239

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.
240+
- The `addEventListener` method can execute multiple functions when a particular event occurs; this is beneficial when building complex or interactive web applications.
241+
In `element.event`, only a single function can be attached to an event.
206242

207243
```js
208244
const button = document.getElementById("button");
@@ -232,12 +268,14 @@ button.onclick = function () {
232268
//👉🏻 only the last event is executed
233269
```
234270

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.
271+
The `addEventListener()` method provides more control over the execution of the event handlers.
272+
You can remove an event using the [`removeEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) method.
236273

237-
## Conclusion
274+
<br />
238275

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.
276+
## Conclusion
240277

278+
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.
241279
A solid understanding of the HTML DOM is essential when building interactive web pages and creating dynamic user interfaces with HTML and JavaScript.
242280

243281
## Resources

0 commit comments

Comments
 (0)