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
<SubHeadingcolor="#25c2a0">JavaScript HTML DOM Simplified, Beginner’s Guide</SubHeading>
8
+
<SubHeadingcolor='#25c2a0'>
9
+
JavaScript HTML DOM Simplified, Beginner’s Guide
10
+
</SubHeading>
9
11
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.
11
13
With DOM, you can create interactive and dynamic user interfaces.
12
14
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.
14
16
15
17
## 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.
17
20
Before going over them, create a simple web page as shown below.
18
21
19
22
<img
@@ -26,74 +29,114 @@ Before going over them, create a simple web page as shown below.
26
29
<h1id="heading">HTML DOM Basics</h1>
27
30
<pid="paragraph">Learn how to manipulate the DOM with JavaScript</p>
28
31
<ulclass="list">
29
-
<liclass="item">JavaScript Beginners</li>
30
-
<liclass="item">JavaScript Intermediates</li>
31
-
<liclass="item">JavaScript Experts</li>
32
+
<liclass="item">JavaScript Beginners</li>
33
+
<liclass="item">JavaScript Intermediates</li>
34
+
<liclass="item">JavaScript Experts</li>
32
35
</ul>
33
36
```
34
37
35
38
### 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.
37
41
The code snippet below selects the HTML element with an id of heading from the web page.
42
+
38
43
```js
39
44
constheading=document.getElementById("heading");
40
45
console.log(heading); //👉🏻 Output: <h1 id="heading">HTML DOM Basics</h1>
41
46
```
42
47
43
48
### 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.
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.
69
81
70
82
### 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
+
72
86
```js
73
87
constitems=document.querySelectorAll(".item");
74
88
console.log(items); //👉🏻 Output: NodeList(3)
75
89
```
76
-
The code snippet above returns all the HTML elements with a class attribute of `item`.
77
90
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
+
79
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.
80
96
81
97
### 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.
83
100
84
101
```js
85
102
constheader=document.querySelector("#heading");
86
-
header.textContent="HTML DOM Manipulation"
103
+
header.textContent="HTML DOM Manipulation";
87
104
```
88
-
The code snippet above changes the content of the `h1` tag to `HTML DOM Manipulation`.
89
105
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.
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.
0 commit comments