-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom-query-js.html
More file actions
187 lines (142 loc) · 6.14 KB
/
dom-query-js.html
File metadata and controls
187 lines (142 loc) · 6.14 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM: Query and Modify</title>
</head>
<body>
<h1 id="main-header">Hello World!</h1>
<ul>
<li data-dbid="1">Item 1</li>
<li data-dbid="2">Item 2</li>
<li data-dbid="3">Item 3</li>
<li data-dbid="4">Item 4</li>
<li data-dbid="5">Item 5</li>
</ul>
<h2 id="sub-header">Hello Again.</h2>
<p class="sub-paragraph">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
<button id="button-1">Change Title</button>
<button id="button-2">Change Subtitle Color</button>
<button id="button-3">Alternate List Items</button>
<button id="button-4">Change Paragraph</button>
<script>
'use strict';
//********Change background color of H1 element***********
// obtain a reference to the target element
// create a handler function
const mainHeader = document.getElementById("main-header");
document.getElementById('main-header').addEventListener("click", () =>
mainHeader.style.backgroundColor = "yellow");
//**********Change paragraph font color**************//
document.querySelector(".sub-paragraph").addEventListener('dblclick', function () {
subParagraphs[0].style.fontSize = "18px";
}
)
//***********Set all li text color to red when the mouse is hovering; reset to black when it is not.**********//
const itemsList = document.getElementsByTagName("li");
document.querySelector("li").addEventListener('mouseenter', function () {
for (let i = 0; i < itemsList.length; i++) {
itemsList[i].style.color = "red";
}
})
document.querySelector("li").addEventListener('mouseleave', function() {
for (let i = 0; i < itemsList.length; i++) {
itemsList[i].style.color = "black";
}
});
// document.querySelector( selector ).addEventListener('mouseenter', handlerIn);
// const listener4 = function () {
// subParagraphs[0].innerHTML = "Mission Accomplished"
// }
// button4.addEventListener('click', listener4)
//**********Button 1************//
//****** Set innerHTML to 'Javascript is cool'****//
// obtain a reference to the target element
// const button1 = document.getElementById("button-1");
// // create a handler function
// const mainHeader = document.getElementById("main-header");
// const listener = () => {
// mainHeader.innerHTML = "Javascript is cool!";
// }
// // register the listener to handle clicks on btn1
// button1.addEventListener('click', listener);
//********Button 2**********//
const button2 = document.getElementById("button-2");
const subHeader = document.getElementById("sub-header");
const listener2 = function () {
subHeader.style.color = "blue";
}
button2.addEventListener('click', listener2);
//*********Button 3*********//
const button3 = document.getElementById("button-3");
// const itemsList = document.getElementsByTagName("li");
const listener3 = function () {
for (let i = 0; i < itemsList.length; i++) {
if (i % 2 !== 1) {
itemsList[i].style.color = "orange"
}
}
}
button3.addEventListener('click', listener3);
//************Button 4***********
//Change Paragraph
const button4 = document.getElementById("button-4")
const subParagraphs = document.getElementsByClassName("sub-paragraph")
const listener4 = function () {
subParagraphs[0].innerHTML = "Mission Accomplished"
}
button4.addEventListener('click', listener4)
// "use strict";
// // TODO: Uncomment next line and get the main header element by id
// const mainHeader = document.getElementById("main-header");
//
// // TODO: Set inner html of mainHeader to "JavaScript is Cool"
// mainHeader.innerHTML = "Javascript is cool"
// TODO: Uncomment the next line and get the sub header element by id
// const subHeader = ;
// const subHeader = document.getElementById("sub-header")
// TODO: Set the text color of subHeader to blue
// subHeader.innerHTML = '<h2 style="color: blue">Hello Again</h2>'
// subHeader.style.color = "blue"
// TODO: Uncomment the next line and get all list items
// const listItems = ;
// const listItems = document.getElementsByTagName("li")
// for(let i = 0; i < listItems.length; i++) {
// console.log(listItems);
// }
// TODO: Set text color on every other list item to grey
// const greyItems = document.getElementsByTagName("li")
// for(let i = 0; i < listItems.length; i++) {
// if(i % 2 !== 0) {
// listItems[i].setAttribute("style", "color: grey")
// }
// }
// console.log(greyItems)
// for(let i = 0; i < listItems.length; i++){
// if(i % 2 !== 1) {
// listItems[i].style.color = "grey"
// }
// }
// TODO: Set text color of li with dbid = 1 to blue
listItems[0].setAttribute("style", "color: blue")
listItems[0].style.color = "blue"
// TODO: Uncomment the next line and get all elements with class name sub-paragraph
// const subParagraphs = ;
// const subParagraphs = document.getElementsByClassName("sub-paragraph")
// console.log(subParagraphs)
// TODO: Change the text in the first sub paragraph to "Mission Accomplished!"
// subParagraphs[0].innerHTML = "Mission Accomplished"
//must specify list of array when using class names - list of elements
// when using ElementByID - there should only be one ID in the html content so does not require index
//get and set attributes
</script>
</body>
</html>