-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonTraversal.html
94 lines (73 loc) · 2.42 KB
/
jsonTraversal.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="css/projects.css">
<title>Kings And Queens - A Json Example</title>
</head>
<body>
<div = "nav">
<nav>
<a href="projects.html">Back To Projects</a>
</nav>
</div>
<div = "topOfPage">
<h3>JSON Traversal</h3>
<h2>Search for Monarchs of the UK!</h2>
</div>
<div = "userInterface">
<p>Type in the box and hit search! Full names not required</p>
<input id="search" type="text" placeholder="Henry" />
<button onclick="search()">Search Away!</button>
</div>
<div id = "tableLocale">
</div>
</body>
<script>
function loadData() {
var requestURL = "https://raw.githubusercontent.com/ewomackQA/JSONDataRepo/master/kings.json";
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json'
request.send();
request.onload = function () {
kingList = request.response;
}
}
var kingList;
function search() {
var searchFor = document.getElementById("search").value;
document.getElementById("tableLocale").innerHTML = `
<table >
<thead>
<td> Ruler </td>
<td> Country </td>
<td> House </td>
<td> Years Reigned </td>
</thead>
<tbody id="kingTable">
</tbody>
</table>
`;
for (var key in kingList) {
if (searchFor.toLowerCase() == "dick")
{
searchFor = "Richard";
}
if (kingList[key].nm.toLowerCase().includes(searchFor.toLowerCase())) {
document.getElementById("kingTable").innerHTML += `
<tr>
<td>${kingList[key].nm}</td>
<td>${kingList[key].cty}</td>
<td>${kingList[key].hse}</td>
<td>${kingList[key].yrs}</td>
</tr> `;
}
}
}
loadData();
</script>
</html>