-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom-query-js.html
182 lines (155 loc) · 6.83 KB
/
dom-query-js.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM: Query and Modify</title>
<script src="js/jquery.js"></script>
</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>
<ul>
<li data-dbid="11">Item 1</li>
<li data-dbid="12">Item 2</li>
<li data-dbid="13">Item 3</li>
<li data-dbid="14">Item 4</li>
<li data-dbid="15">Item 5</li>
</ul>
<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 1</button>
<button id="btn2">Button 2</button>
<button id="btn3">Button 3</button>
<button id="btn4">Button 4</button>
<script>
(function () {
'use strict';
// create a handler function
var listener = function (event) {
// 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";
}
// register the listener to handle clicks on btn1
document.getElementById('btn1').addEventListener('click', listener, false);
var listener2 = function (event) {
// 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";
}
// register the listener to handle clicks on keyup at the document level
document.getElementById('btn2').addEventListener('click', listener2, false);
var listener3 = function (event) {
// TODO: Uncomment the next line and get all list items
var listItems = document.getElementsByTagName("li");
// TODO: Set text color on every other list item to grey
for (var i = 0; i < listItems.length; i += 2) {
listItems[i].style.color = "gray";
}
// TODO: Set text color of li with dbid = 1 to blue
for (var i = 0; i < listItems.length; i++) {
if (listItems[i].getAttribute("data-dbid") == "1") {
listItems[i].style.color = "blue";
}
}
}
document.getElementById('btn3').addEventListener("click", listener3, false);
var listener4 = function (event) {
// TODO: Uncomment the next line and get all elements with class name sub-paragraph test
var subParagraphs = document.getElementsByClassName("sub-paragraph");
// TODO: Change the text in the first sub paragraph to "Mission Accomplished!"
subParagraphs[0].innerHTML = "Mission Accomplished!";
}
// // register the listener to handle clicks on btn1
document.getElementById('btn4').addEventListener('click', listener4, false);
//
// "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";
// // mainHeader.innerText = "JavaScript is Cool";
// // console.log(mainHeader);
//
// // 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");
//
// // TODO: Set text color on every other list item to grey
// for (var i = 0; i < listItems.length; i += 2) {
// listItems[i].style.color = "gray";
// }
// // TODO: Set text color of li with dbid = 1 to blue
// for (var i = 0; i < listItems.length; i++) {
// if (listItems[i].getAttribute("data-dbid") == "1") {
// listItems[i].style.color = "blue";
// }
//
// }
//
// document.querySelector('li["data-dbid"]="4"').style.color = "purple";
//
// // TODO: Uncomment the next line and get all elements with class name sub-paragraph
// var subParagraphs = document.getElementsByClassName("sub-paragraph");
//
// // TODO: Change the text in the first sub paragraph to "Mission Accomplished!"
// subParagraphs[0].innerHTML = "Mission Accomplished!";
// When a user hits the "g" key, the background of the page should turn purple.
// document.getElementById('body').addEventListener("keyUp", function(){document.querySelector('body').style.color = "purple";});
// $( "#target" ).keyup(function() {
// alert( "Handler for .keyup() called." );
// });
$('body').keyup(function () {
//document.querySelector('body').style.backgroundColor = "purple";
console.log(this);
$('body').css('background-color', "purple"); // Very light grey
// We had to use [] syntax since the property name has a dash
}
);
// document.querySelector("#button-3").addEventListener("click", function() {
// for (var i = 0; i < listItems.length; i += 1) {
// var dbidValue = listItems[i].getAttribute("data-dbid");
// if (dbidValue === "1") {
// listItems[i].style.color = "blue";
// } else if (i % 2 === 0) {
// listItems[i].style.color = "grey";
// }
// }
// });
// Every time the enter key is pressed, increment and display a counter that shows the total number of times the enter key has been pressed.
// When someone types 1, 2, 3 in a row, show an alert that says "You get a 50% discount!".
})();
</script>
</body>
</html>