-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage04.html
132 lines (125 loc) · 5.32 KB
/
page04.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Petfinder API | Page 4</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="css/styles.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<a class="navbar-brand" href="index.html">Home</a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Menu<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="index.html">Home</a></li>
<li><a href="page01.html">1. API Keys and Making Requests</a></li>
<li><a href="page02.html">2. Cross-Domain Requests & JSONP</a></li>
<li><a href="page03.html">3. The pet.get Method & the DOM</a></li>
<li><a href="page04.html">4. Adding Page Content</a></li>
<li><a href="page05.html">5. Adding Images</a></li>
<li><a href="page06.html">6. Getting a Random Cat</a></li>
<li><a href="page07.html">7. Adding a Button and Event Listener</a></li>
<li><a href="page08.html">8. Conclusion</a></li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<div class="container-fluid">
<div class="jumbotron">
<h1>Adding Page Content</h1>
<p>Accessing DOM elements to append strings from JSON objects</p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12"><p>We can use the table cell's IDs to place the content. The breeds object contains an array called breed, so we'll need to loop through the objects in the array to access each of them, and append them to an empty unordered list.</p>
<img src="images/breed_array.png" class="img-responsive" alt="Array of Breeds"><br/>
<div class="row">
<div class="col-lg-6"><table>
<tbody>
<tr>
<th scope="col"><h3>Cat of the Day</h3></th>
<th scope="col"> </th>
</tr>
<tr>
<td><p>Name:</p></td>
<td id="name"> </td>
</tr>
<tr>
<td><p>Age:</p></td>
<td id="age"> </td>
</tr>
<tr>
<td><p>Sex:</p></td>
<td id="sex"> </td>
</tr>
<tr>
<td><p>Breed:</p></td>
<td id="breed">
<ul id = "breedlist"></ul>
</td>
</tr>
<tr>
<td><p>City:</p></td>
<td id="city"> </td>
</tr>
</tbody>
</table></div>
<div class="col-lg-6"><pre>function getKitty(data) {
var response = data.petfinder.pet;
document.getElementById('name').textContent = response.name.$t;
document.getElementById('age').textContent = response.age.$t;
document.getElementById('sex').textContent = response.sex.$t;
for(var i = 0; i < response.breeds.breed.length; i++){
var newItem = document.createElement("li");
var textNode = document.createTextNode(response.breeds.breed[i].$t);
newItem.appendChild(textNode);
document.getElementById('breedlist').appendChild(newItem);
}
document.getElementById('city').textContent = response.contact.city.$t + ', ' + response.contact.state.$t;
} </pre></div>
</div><br/>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-4">
<a href="page03.html"><button type="button" class="btn btn-info">Prev.</button></a>
<a href="page05.html"><button type="button" class="btn btn-info">Next</button></a>
</div>
</div>
<hr>
<div class="row">
<div class="text-center col-md-6 col-md-offset-3">
<p>Copyright © Morgan Brenner · 2016 </p>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="js/credentials.js"></script>
<script src="js/p4_ex.js"></script>
</body>
</html>