-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (54 loc) · 1.93 KB
/
script.js
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
"use stric";
//USING AJAX
const container = document.querySelector(".container");
const btn = document.querySelector(".btn");
const renderCountry = function (data) {
const html = `<h2 class="country_name">${data.Country_Region}</h2>
<p class="confirmed">Confirmed cases: ${data.Confirmed}</p>
<p class="deaths">Deaths: ${data.Deaths}</p>`;
container.insertAdjacentHTML("beforeend", html);
};
const getCountryData = function (countryName) {
const request = new XMLHttpRequest();
request.open("GET", "https://coronavirus.m.pipedream.net/");
request.send();
request.addEventListener("load", function () {
const data = JSON.parse(request.responseText);
const countryData = data.rawData.find(
(item) => item.Country_Region === countryName
);
if (countryData) {
renderCountry(countryData);
document.querySelector(".country").value = "";
}
});
};
btn.addEventListener("click", function () {
const countryName = document.querySelector(".country").value;
getCountryData(countryName);
});
/* USING FETCH
const container = document.querySelector(".container");
const btn = document.querySelector(".btn");
const renderCountry = function (data) {
const html = `<h2 class="country_name">${data.Country_Region}</h2>
<p class="confirmed">Confirmed cases: ${data.Confirmed}</p>
<p class="deaths">Deaths: ${data.Deaths}</p>`;
container.insertAdjacentHTML("beforeend", html);
};
const getCountryData = async function (countryName) {
await fetch("https://coronavirus.m.pipedream.net/")
.then((data) => {
return data.json();
})
.then((response) => {
renderCountry(
response.rawData.find((item) => item.Country_Region === countryName)
);
document.querySelector(".country").value = "";
});
};
btn.addEventListener("click", function () {
const countryName = document.querySelector(".country").value;
getCountryData(countryName);
});*/