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
Copy file name to clipboardExpand all lines: docs/technologies/javascript/html-dom-simplified.mdx
+75-37Lines changed: 75 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,12 +9,14 @@ import SubHeading from "@site/src/components/SubHeading";
9
9
JavaScript HTML DOM Simplified, Beginner’s Guide
10
10
</SubHeading>
11
11
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**.
13
13
With DOM, you can create interactive and dynamic user interfaces.
14
14
15
15
In this article, you’ll learn how to create event listeners and select, create, modify, and delete elements and attributes on a web page.
16
16
17
-
## Accessing HTML DOM elements in JavaScript
17
+
<br />
18
+
19
+
## Accessing DOM elements in `JavaScript`
18
20
19
21
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.
20
22
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.
35
37
</ul>
36
38
```
37
39
38
-
### Using `document.getElementById()`
40
+
<br />
41
+
42
+
### 👉 Using `getElementById()`
39
43
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.
41
45
The code snippet below selects the HTML element with an id of heading from the web page.
42
46
43
47
```js
44
48
constheading=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>
46
50
```
47
51
48
-
### Using `document.getElementsByClassName()`
52
+
<br />
53
+
54
+
### 👉 Using `getElementsByClassName()`
49
55
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.
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.
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`.
81
92
82
-
### Using `document.querySelectorAll()`
93
+
<br />
83
94
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.
85
99
86
100
```js
87
101
constitems=document.querySelectorAll(".item");
88
-
console.log(items); //👉🏻 Output: NodeList(3)
102
+
console.log(items); //👉🏻 Output: NodeList(3)
89
103
```
90
104
91
105
The code snippet above returns all the HTML elements with a class attribute of `item`.
92
106
93
-
## Modifying HTML DOM elements
107
+
<br />
108
+
109
+
## `Modifying` DOM Elements
94
110
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`.
96
113
97
-
### Using the `textContent` property
114
+
<br />
115
+
116
+
### 👉 Using the `textContent` property
98
117
99
118
The `textContent` property allows you to change only the content of an HTML element.
100
119
@@ -105,7 +124,9 @@ header.textContent = "HTML DOM Manipulation";
105
124
106
125
The code snippet above changes the content of the `h1` tag to "HTML DOM Manipulation".
107
126
108
-
### Using the `innerHTML` property
127
+
<br />
128
+
129
+
### 👉 Using the `innerHTML` property
109
130
110
131
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.
- creates a new `li` element, sets its content, and adds a class attribute to the `li` element, then,
140
163
- appends the `li` element to the `ul` tag on the web page.
141
164
142
-
## Removing elements from the HTML DOM in JavaScript
165
+
<br />
166
+
167
+
## `Removing Elements` from the HTML DOM
143
168
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.
145
171
146
172
```js
147
173
constlist=document.querySelector(".list");
@@ -153,9 +179,12 @@ list.removeChild(firstItem);
153
179
154
180
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
181
156
-
## Listening to HTML DOM events with JavaScript
182
+
<br />
157
183
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.
159
188
160
189
Add a button element to the web page. We'll add event listeners to the button.
161
190
@@ -170,9 +199,11 @@ Add a button element to the web page. We'll add event listeners to the button.
170
199
<buttonid="button">Hello Reader</button>
171
200
```
172
201
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 />
174
205
175
-
### Using the `addEventListener()` method
206
+
### 👉 Using the `addEventListener()` method
176
207
177
208
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
209
@@ -186,7 +217,9 @@ button.addEventListener("click", function () {
186
217
187
218
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
219
189
-
### Using the `element.event` method
220
+
<br />
221
+
222
+
### 👉 Using the `element.event` method
190
223
191
224
It is an older way of adding event listeners to HTML elements in JavaScript. It assigns a function to an HTML element.
192
225
@@ -200,9 +233,12 @@ button.onclick = function () {
200
233
201
234
The `button.onclick` method may look straightforward compared to the `addEventListener` method, but it has some limitations.
202
235
203
-
### `addEventListener()` vs `element.event`
236
+
<br />
237
+
238
+
### 👉 `addEventListener()` vs `element.event`
204
239
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.
206
242
207
243
```js
208
244
constbutton=document.getElementById("button");
@@ -232,12 +268,14 @@ button.onclick = function () {
232
268
//👉🏻 only the last event is executed
233
269
```
234
270
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.
236
273
237
-
## Conclusion
274
+
<br />
238
275
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
240
277
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.
241
279
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