forked from Shubham56-droid/Web-Components-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpet.js
More file actions
27 lines (23 loc) · 930 Bytes
/
pet.js
File metadata and controls
27 lines (23 loc) · 930 Bytes
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
var xhr = new XMLHttpRequest();
xhr.open('GET', 'pet.json', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var items = JSON.parse(xhr.responseText);
var output = '';
for (var i in items) {
output += '<p>' + items[i].name + " is "+ items[i].age + " years old and is "
+ items[i].gender + ". It is " + items[i].breed + " and weighs " + items[i].weight +
". Favourite food is " + items[i].food + "." + '</p>';
if (i == 0) {
document.getElementById('dog1').innerHTML = output;
output = ""
} else if (i == 1) {
document.getElementById('dog2').innerHTML = output;
output = ""
} else {
document.getElementById('dog3').innerHTML = output;
}
}
}
}
xhr.send();