-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexJQuery.html
323 lines (293 loc) · 10.8 KB
/
indexJQuery.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>DOM</title>
</head>
<body>
<div>
<div>
<h3>Sum: <span id="sum"></span></h3>
<h5 id="currentTime"></h5>
</div>
<div>
<h1>United <span data-customAttr="USA">States</span> of America (<span>USA</span>)</h1>
<div><a>Click Here</a><a>Click Here</a><a>Click Here</a><a>Click Here</a><a>Click Here</a></div>
<a>Click Here</a>
<a>Click <span>Here1</span></a>
<a>Click Here</a>
<div>
<select name="colors">
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="red" selected="selected">Red</option>
<option value="purple">Purple</option>
<option value="yellow" selected="selected">Yellow</option>
</select>
</div>
</div>
<p id="helloWorld">Hello, World!</p>
<div>
<table>
<tr>
<th>Name</th>
<th>Department</th>
</tr>
<tr>
<td class="empName">John</td>
<td>Sales</td>
</tr>
<tr>
<td class="empName">Amy</td>
<td>Finance</td>
</tr>
<tr>
<td class="empName">Austin</td>
<td>Sales</td>
</tr>
<tr>
<td class="empName">Katie</td>
<td>Marketing</td>
</tr>
<tr>
<td class="empName" data-customAttr="court">Courtney</td>
<td>Sales</td>
</tr>
<tr>
<td class="empName">Scout</td>
<td>Sales</td>
</tr>
</table>
</div>
<form id="firstForm">
<a>Click <span>Here2</span></a>
<input type="text" data-customAttr="7" /><br />
<input type="text" data-customAttr="24" /><br />
<input type="radio" name="favoriteAnimal" value="dog" />Dog<br />
<input type="radio" name="favoriteColor" value="blue" />Blue<br />
<input id="num1" class="nums" type="text" /><br />
<input type="radio" name="favoriteAnimal" value="cat" />Cat<br />
<input type="radio" name="favoriteColor" value="red" />Red<br />
<input type="radio" name="favoriteAnimal" value="horse" />Horse<br />
<input type="radio" name="favoriteAnimal" value="dolphin" />Dolphin<br />
<a>Click <span>Here</span></a><br />
<input type="radio" name="favoriteAnimal" value="eagle" />Eagle<br />
<input type="radio" name="favoriteColor" value="green" />Green<br />
<input type="radio" name="favoriteColor" value="orange" />Orange<br />
<select name="hobbies">
<option value="photography">Photography</option>
<option value="football" selected="selected">Football</option>
<option value="kayaking" selected="selected">Kayaking</option>
<option value="hiking">Hiking</option>
<option value="programming" selected="selected">Programming</option>
</select>
<input id="num2" class="nums" type="text" />
<select name="skills">
<option value="java">Java</option>
<option value="net">.NET</option>
<option value="dom" selected="selected">DOM</option>
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="javascript" selected="selected">Javascript</option>
</select>
</form>
</div>
<a>Click Here</a>
<a>Click <span data-customAttr="500">Here3</span></a>
<a>Click Here</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>
<script>
$(document).ready(function () {
// 1. USA
// Define function getUSA()
// Find the html element that contains "USA".
// Print that element's contents.
function getUSA() {
let $collection = $("h1 span:nth-child(2)").text();
console.log($collection);
}
// getUSA();
// 2. Sales
// Define function getPeopleInSales()
// Print the names of all the people in the sales department.
function getPeopleInSales() {
let $sales = $("td").toArray();
for (let index = 0; index < $sales.length; index++) {
if ($sales[index].innerHTML == "Sales") {
console.log($sales[index - 1].innerHTML);
}
}
}
// getPeopleInSales();
// 3. Click Here
// Define function getAnchorChildren()
// Find all anchor elements with a <span> child.
// Print the contents of <span>
function getAnchorChildren() {
let $anchor = $("a span");
for (let index = 0; index < $anchor.length; index++) {
console.log($anchor[index].innerHTML);
}
}
// getAnchorChildren()
// 4. Hobbies
// Define function getHobbies()
// Find the checked option in the 'hobbies' select element.
// Print the value and the contents.
$('select[name$="hobbies"]').change(function getHobbies() {
let value = $(this).children("option:selected").val();
let contents = $(this).children("option:selected").text();
console.log("Value: " + value);
console.log("\tContents:" + contents);
});
// 5. Custom Attribute
// Define function getCustomAttribute()
// Find all elements with "data-customAttr" attribute
// Print the value of the attribute.
// Print the element that has the attribute.
$("[data-customAttr]").each(function getCustomAttribute() {
console.log($(this).attr("data-customAttr"));
console.log($(this));
})
// 6. Sum Event
// NOTE: Write unobtrusive Javascript
// Regarding these elements:
// <input id="num1" class="nums" type="text"/>
// <input id="num2" class="nums" type="text"/>
// <h3>Sum: <span id="sum"></span></h3>
// Define onchange event handler.
// Add <input> element values.
// Put the sum in the <span> element.
// If values cannot be added, put "Cannot add" in the <span> element
let sum = 0;
let num1 = 0;
let num2 = 0;
$("input #num1").change(function () {
num1 = $("#num1").val();
// console.log();
num2 = $("#num2").val();
sum = parseInt(num1) + parseInt(num2);
if (sum) {
$("#sum").val(sum);
} else {
$("#sum").text("Cannot Add");
}
})
$("#num2").change(function () {
num1 = $("#num1").val();
num2 = $("#num2").val();
sum = parseInt(num1) + parseInt(num2);
if (sum) {
$("#sum").text(sum);
} else {
$("#sum").text("Cannot Add");
}
})
// 7. Skills Event
// NOTE: Write unobtrusive Javascript
// When user selects a skill, create an alert with a message similar to:
// "Are you sure CSS is one of your skills?"
// NOTE: no alert should appear when user deselects a skill.
$('select[name$="skills"]').change(function () {
let contents = $(this).children("option:selected").text();
alert("Are you sure " + contents + " is one of your skills?");
})
// 8. Favorite Color Event
// NOTE: Write unobtrusive Javascript
// NOTE: This is regarding the favoriteColor radio buttons.
// When a user selects a color, create an alert with a message similar to:
// "So you like green more than blue now?"
// In this example, green is the new value and blue is the old value.
// Make the background color (of all favoriteColor radio buttons) the newly selected favoriteColor
let previousColor = "white";
$('input[name$="favoriteColor"]').click(function (params) {
let currentColor = $('input[name$="favoriteColor"]:checked').val();
// $(this).wrap("<div style='display:inline-block; background-color:'" + currentColor + "></div>");
alert("So you like " + currentColor + " more than " + previousColor + " now?");
$('input[name$="favoriteColor"]').each(function (params) {
$(this).css("backgroundColor", currentColor);
})
previousColor = currentColor;
})
// 9. Show/Hide Event
// NOTE: Write unobtrusive Javascript
// When user hovers over an employees name:
// Hide the name if shown.
// Show the name if hidden.
$("td").mouseover(function (params) {
if ($(this).hasClass("empName")) {
// alert($(this).css("color"))
if ($(this).css("color") === "rgb(0, 0, 0)") {
$(this).css("color", "white");
} else {
$(this).css("color", "black");
}
}
});
// 10. Current Time
// Regarding this element:
// <h5 id="currentTime"></h5>
// Show the current time in this element in this format: 9:05:23 AM
// The time should be accurate to the second without having to reload the page.
setInterval(function () {
let time = new Date();
let hour = time.getHours();
let mins = time.getMinutes();
let sec = time.getSeconds();
let amPm = "AM";
if (hour > 12) {
amPm = "PM";
hour -= 12;
}
if (hour === 0) {
hour = 12;
}
if (mins < 10) {
mins = "0" + mins;
}
if (sec < 10) {
sec = "0" + sec;
}
$("#currentTime").text(hour + ":" + mins + ":" + sec + " " + amPm);
}, 1000);
// 11. Delay
// Regarding this element:
// <p id="helloWorld">Hello, World!</p>
// Three seconds after a user clicks on this element, change the text to a random color.
// setInterval(function (params) {
$("#helloWorld").click(function () {
let random = Math.floor(Math.random() * 16777215).toString(16);
let randColor = "#" + random;
setTimeout(function () {
$("#helloWorld").css("color", randColor)
}, 3000);
$("#mydiv").css("color", "green")
});
// **************************************This left to do****************************************************************
// 12. Walk the DOM
// Define function walkTheDOM(node, func)
// This function should traverse every node in the DOM. Use recursion.
// On each node, call func(node).
function walkTheDom(node, func) {
func(node);
}
function diveTheDom(node) {
if (node.tagName !== undefined) {
if (node.text === undefined) {
console.log("The nodes tag is: " + node.tagName + "\n\tThe nodes name is: " + node.name);
} else {
console.log("The nodes tag is: " + node.tagName + " \n\tThe nodes value is: " + node.text);
}
}
if (node.hasChildNodes) {
let nodeList = node.childNodes;
for (let i = 0; i < nodeList.length; i++) {
diveTheDom(nodeList[i]);
}
}
}
walkTheDom(document.body, diveTheDom)
});
</script>
</body>
</html>