Skip to content

Commit 6b9d0ef

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

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

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

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ description: JavaScript HTML DOM Simplified, for Beginners
55

66
import SubHeading from "@site/src/components/SubHeading";
77

8-
<SubHeading color="#25c2a0">JavaScript HTML DOM Simplified, Beginner’s Guide</SubHeading>
8+
<SubHeading color='#25c2a0'>
9+
JavaScript HTML DOM Simplified, Beginner’s Guide
10+
</SubHeading>
911

10-
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.
1113
With DOM, you can create interactive and dynamic user interfaces.
1214

13-
In this article, you’ll learn how to create event listeners and select, create, modify, and delete elements and attributes on a web page.
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.
1416

1517
## Accessing HTML DOM elements in JavaScript
16-
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.
18+
19+
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.
1720
Before going over them, create a simple web page as shown below.
1821

1922
<img
@@ -26,74 +29,114 @@ Before going over them, create a simple web page as shown below.
2629
<h1 id="heading">HTML DOM Basics</h1>
2730
<p id="paragraph">Learn how to manipulate the DOM with JavaScript</p>
2831
<ul class="list">
29-
<li class="item">JavaScript Beginners</li>
30-
<li class="item">JavaScript Intermediates</li>
31-
<li class="item">JavaScript Experts</li>
32+
<li class="item">JavaScript Beginners</li>
33+
<li class="item">JavaScript Intermediates</li>
34+
<li class="item">JavaScript Experts</li>
3235
</ul>
3336
```
3437

3538
### Using `document.getElementById()`
36-
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.
39+
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.
3741
The code snippet below selects the HTML element with an id of heading from the web page.
42+
3843
```js
3944
const heading = document.getElementById("heading");
4045
console.log(heading); //👉🏻 Output: <h1 id="heading">HTML DOM Basics</h1>
4146
```
4247

4348
### Using `document.getElementsByClassName()`
44-
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.
49+
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.
4551

4652
```js
4753
const item = document.getElementsByClassName("item");
4854
console.log(item); //👉🏻 Output: HTMLCollection(3)
4955
```
5056

5157
### Using `document.getElementsByTagName()`
52-
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.
53-
```js
58+
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.
60+
61+
```js
5462
const paragraph = document.getElementsByTagName("p");
5563
console.log(paragraph); //👉🏻 Output: HTMLCollection(1)
56-
```
64+
```
65+
5766
The `paragraph` variable contains an array of all the paragraph tags on the web page.
5867

5968
### Using `document.querySelector()`
60-
With this method, you can access an element via its CSS selector. It returns the first element that matches the selector.
69+
70+
With this method, you can access an element via its CSS selector. It returns the first element that matches the selector.
71+
6172
```js
6273
const paragraph = document.querySelector("#paragraph");
6374
console.log(subHeading); //👉🏻 Output: <p id="paragraph">Learn how to manipulate the DOM with JavaScript</p>
6475

6576
const list = document.querySelector(".list");
6677
console.log(list); //👉🏻 Output: <ul class="list">...</ul>
67-
```
68-
The code snippet above shows you can reference an element via its ID or class attributes.
78+
```
79+
80+
The code snippet above shows you can reference an element via its ID or class attributes.
6981

7082
### Using `document.querySelectorAll()`
71-
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.
83+
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.
85+
7286
```js
7387
const items = document.querySelectorAll(".item");
7488
console.log(items); //👉🏻 Output: NodeList(3)
7589
```
76-
The code snippet above returns all the HTML elements with a class attribute of `item`.
7790

78-
## Modifying HTML DOM elements
91+
The code snippet above returns all the HTML elements with a class attribute of `item`.
92+
93+
## Modifying HTML DOM elements
94+
7995
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.
8096

8197
### Using the `textContent` property
82-
The `textContent` property allows you to change only the content of an HTML element.
98+
99+
The `textContent` property allows you to change only the content of an HTML element.
83100

84101
```js
85102
const header = document.querySelector("#heading");
86-
header.textContent = "HTML DOM Manipulation"
103+
header.textContent = "HTML DOM Manipulation";
87104
```
88-
The code snippet above changes the content of the `h1` tag to `HTML DOM Manipulation`.
89105

90-
### Using the `innerHTML` property
91-
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.
92-
```js.
106+
The code snippet above changes the content of the `h1` tag to `HTML DOM Manipulation`.
107+
108+
### Using the `innerHTML` property
109+
110+
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.
111+
112+
```js
93113
const paragraph = document.getElementById("paragraph");
94-
paragraph.innerHTML = "<h3>DOM Manipulation made easy</h3>"
114+
paragraph.innerHTML = "<h3>DOM Manipulation made easy</h3>";
115+
```
116+
117+
## Adding elements to the HTML DOM in JavaScript
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.
120+
121+
You can add a new list item to the `ul` tag on the web page as done below.
122+
123+
```js
124+
const list = document.querySelector(".list");
125+
const item = document.createElement("li");
126+
127+
item.textContent = "Newly added";
128+
item.className = "item";
129+
130+
list.appendChild(item);
131+
console.log(item); //👉🏻 Output: <li class="item">Newly added</li>
95132
```
96-
<br />
133+
134+
The code snippet above,
135+
136+
- gets the `ul` element from the web page,
137+
- creates a new `li` element, sets its content, and adds a class attribute to the `li` element, then,
138+
- appends the `li` element to the `ul` tag on the web page.
139+
<br />
97140

98141
## Resources
99142

0 commit comments

Comments
 (0)