-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom-query-js.html
More file actions
115 lines (106 loc) · 4.36 KB
/
dom-query-js.html
File metadata and controls
115 lines (106 loc) · 4.36 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
<!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="btn1"></button>
<button id="btn2"></button>
<button id="btn3"></button>
<button id="btn4"></button>
<p id="para"></p>
<script>
(function () {
"use strict";
// TODO: Uncomment next line and get the main header element by id
var mainHeader = document.getElementById('main-header');
// TODO: Set inner html of mainHeader to "JavaScript is Cool"
// mainHeader.innerHTML = 'Javascript is Cool';
// console.log(mainHeader.innerText);
// console.log(mainHeader.innerHTML);
var btn1 = document.getElementById('btn1');
btn1.setAttribute('style', 'padding: 5px');
btn1.addEventListener('click', listener1);
function listener1() {
mainHeader.innerHTML = 'Javascript is Cool';
}
var btn2 = document.getElementById('btn2');
btn2.setAttribute('style', 'padding: 10px');
btn2.addEventListener('click', listener2);
function listener2(event) {
document.getElementById('para').innerText = 'The End';
document.getElementById('para').style.fontSize = '3rem';
}
var btn3 = document.getElementById('btn3');
btn3.setAttribute('style', 'padding: 15px; border: 5px solid #aaa');
btn3.addEventListener('click', listener3);
function listener3() {
;
}var btn4 = document.getElementById('btn4');
btn4.setAttribute('style', 'padding: 20px; background-color: pink');
btn4.addEventListener('click', listener4);
function listener4() {
btn1.removeEventListener('click', listener1);
console.log('removed');
mainHeader.innerHTML = 'Hello World!';
}
// TODO: Uncomment the next line and get the sub header element by id
var subHeader = document.getElementById('sub-header');
// TODO: Set the text color of subHeader to blue
subHeader.style.color = 'blue';
// TODO: Uncomment the next line and get all list items
var listItems = document.getElementsByTagName('li');
// console.log(listItems)
// console.log(listItems[0])
// console.log(listItems[0].getAttribute("data-dbid"))
// TODO: Set text color on every other list item to grey
// TODO: Set text color of li with dbid = 1 to blue
// for (var i = 0; i < listItems.length; i++) {
// var dbID = parseFloat(listItems[i].getAttribute("data-dbid"));
// if (dbID % 2 !== 0) {
// listItems[i].style.color = '#bbb';
// }
// if (dbID === 1) {
// listItems[i].style.color = 'blue';
// }
// }
for (var i = 0; i < listItems.length; i++) {
var dbID = listItems[i].getAttribute("data-dbid");
if (dbID % 2 != 0) {
listItems[i].style.color = '#bbb';
}
if (dbID == 1) { // (dbID === '1')
listItems[i].style.color = 'blue';
}
}
// TODO: Uncomment the next line and get all elements with class name sub-paragraph
var subParagraphs = document.getElementsByClassName("sub-paragraph");
// console.log(subParagraphs);
console.log(subParagraphs[0]);
// TODO: Change the text in the first sub paragraph to "Mission Accomplished!"
subParagraphs[0].innerHTML = 'Mission Accomplished';
// console.log(subParagraphs[0].innerHTML);
// console.log(subParagraphs[0].innerText);
})();
</script>
</body>
</html>