Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (42 loc) · 1.56 KB
/
Copy pathapp.js
File metadata and controls
52 lines (42 loc) · 1.56 KB
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
fetch('https://restcountries.com/v2/all')
.then(res => res.json())
.then(data => {
displayCountry(data);
})
const displayCountry = country => {
const parentNode = document.getElementById('countries');
country.forEach(countryInfo => {
const countryDiv = document.createElement('div');
countryDiv.className = "single";
const nameCity = `<h3 class='countryName'>${countryInfo.name}</h3>
<p class='city'>City: ${countryInfo.capital}</p>
<button onclick="countryDetails('${countryInfo.name}')">Details</button>`;
countryDiv.innerHTML = nameCity;
parentNode.appendChild(countryDiv);
// for (let i = 0; i < country.length; i++) {
// const countryInfo = country[i];
// const name = document.createElement('h1');
// name.innerText = countryInfo.name;
// const city = document.createElement('h3');
// city.innerText = countryInfo.capital;
// countryDiv.appendChild(name);
// countryDiv.appendChild(city);
});
}
const countryDetails = country => {
const url = `https://restcountries.com/v2/name/${country}`
fetch(url)
.then(res => res.json())
.then(data => renderCountryInfo(data[0]))
}
const renderCountryInfo = name => {
const countryAbout = document.getElementById('countryDetails');
const details = `
<h1>Country: ${name.name} </h1>
<img src=${name.flag}>
<p>Area: ${name.area}</p>
<h3>Population: ${name.population}</h3>
`;
countryAbout.innerHTML = details;
window.scroll(0,0);
}