-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 9 DOM.html
210 lines (196 loc) · 5.61 KB
/
Day 9 DOM.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!-- Activity 1: Selecting and Manipulating Elements -->
<!-- Task 1: Select an HTML element by its ID and change its text content. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 1</title>
</head>
<body>
<p id="task1">Original Text</p>
<script>
document.getElementById('task1').textContent = 'Changed Text';
</script>
</body>
</html>
<!-- Task 2: Select an HTML element by its class and change its background color. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 2</title>
<style>
.task2 {
background-color: white;
}
</style>
</head>
<body>
<div class="task2">Background Color Change</div>
<script>
document.querySelector('.task2').style.backgroundColor = 'lightblue';
</script>
</body>
</html>
<!-- Activity 2: Creating and Appending Elements -->
<!-- Task 3: Create a new `div` element with some text content and append it to the body. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 3</title>
</head>
<body>
<script>
const newDiv = document.createElement('div');
newDiv.textContent = 'This is a new div';
document.body.appendChild(newDiv);
</script>
</body>
</html>
<!-- Task 4: Create a new `li` element and add it to an existing `ul` list. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 4</title>
</head>
<body>
<ul id="task4-list">
<li>Existing Item 1</li>
<li>Existing Item 2</li>
</ul>
<script>
const newLi = document.createElement('li');
newLi.textContent = 'New List Item';
document.getElementById('task4-list').appendChild(newLi);
</script>
</body>
</html>
<!-- Activity 3: Removing Elements -->
<!-- Task 5: Select an HTML element and remove it from the DOM. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 5</title>
</head>
<body>
<p id="task5">This element will be removed</p>
<script>
const elementToRemove = document.getElementById('task5');
elementToRemove.parentNode.removeChild(elementToRemove);
</script>
</body>
</html>
<!-- Task 6: Remove the last child of a specific HTML element. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 6</title>
</head>
<body>
<ul id="task6-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script>
const list = document.getElementById('task6-list');
list.removeChild(list.lastElementChild);
</script>
</body>
</html>
<!-- Activity 4: Modifying Attributes and Classes -->
<!-- Task 7: Select an HTML element and change one of its attributes (e.g., `src` of an `img` tag). -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 7</title>
</head>
<body>
<img id="task7" src="old-image.jpg" alt="Old Image">
<script>
document.getElementById('task7').setAttribute('src', 'new-image.jpg');
</script>
</body>
</html>
<!-- Task 8: Add and remove a CSS class to/from an HTML element. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 8</title>
<style>
.new-class {
color: red;
}
</style>
</head>
<body>
<p id="task8">This is a paragraph.</p>
<script>
const paragraph = document.getElementById('task8');
paragraph.classList.add('new-class');
setTimeout(() => {
paragraph.classList.remove('new-class');
}, 3000); // Remove the class after 3 seconds
</script>
</body>
</html>
<!-- Activity 5: Event Handling -->
<!-- Task 9: Add a click event listener to a button that changes the text content of a paragraph. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 9</title>
</head>
<body>
<p id="task9-paragraph">Original Text</p>
<button id="task9-button">Change Text</button>
<script>
document.getElementById('task9-button').addEventListener('click', () => {
document.getElementById('task9-paragraph').textContent = 'Text Changed!';
});
</script>
</body>
</html>
<!-- Task 10: Add a mouseover event listener to an element that changes its border color. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 10</title>
<style>
.task10-element {
border: 2px solid black;
padding: 10px;
}
</style>
</head>
<body>
<div class="task10-element">Hover over me</div>
<script>
const element = document.querySelector('.task10-element');
element.addEventListener('mouseover', () => {
element.style.borderColor = 'blue';
});
element.addEventListener('mouseout', () => {
element.style.borderColor = 'black';
});
</script>
</body>
</html>