Repository navigation
Expand file tree
/
Copy pathcountry.js
More file actions
72 lines (69 loc) · 2.89 KB
/
Copy pathcountry.js
File metadata and controls
72 lines (69 loc) · 2.89 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let searchBtn = document.getElementById("search-btn");
let countryinp = document.getElementById("country-inp");
let result = document.getElementById("result");
searchBtn.addEventListener("click",()=>{
let countryName = countryinp.value ;
let finalURL = `https://restcountries.com/v3.1/name/${countryName}?fullText=true`;
console.log(finalURL);
fetch(finalURL)
.then((response) => response.json())
.then((data) => {
// console.log(data[0]);
// console.log(data[0].capital[0]);
// console.log(data[0].flags.svg);
// console.log(data[0].name.common);
// console.log(data[0].continents[0]);
// console.log(data[0].population);
// console.log(Object.keys(data[0].currencies)[0]);
// console.log(data[0].currencies[Object.keys(data[0].currencies)].name);
// console.log(Object.values(data[0].languages).toString().split(",").join(", "));
result.innerHTML = `
<img src="${data[0].flags.svg}" class="flag-img">
<h2>${data[0].name.common}</h2>
<div class="wrapper">
<div class="data-wrapper">
<h4>Capital : </h4>
<span>${data[0].capital[0]}</span>
</div>
</div>
<div class="wrapper">
<div class="data-wrapper">
<h4>Continents : </h4>
<span>${data[0].continents[0]}</span>
</div>
</div>
<div class="wrapper">
<div class="data-wrapper">
<h4>Population : </h4>
<span>${data[0].population}</span>
</div>
</div>
<div class="wrapper">
<div class="data-wrapper">
<h4>Currency : </h4>
<span>${data[0].currencies[Object.keys(data[0].currencies)].name} - ${Object.keys(data[0].currencies)[0]}</span>
</div>
</div>
<div class="wrapper">
<div class="data-wrapper">
<h4>Common Languages : </h4>
<span>${Object.values(data[0].languages).toString().split(",").join(", ")}</span>
</div>
</div>
<button id="clear-btn">Close</button>
`;
})
.catch(()=>{
if(countryName.length == 0){
result.innerHTML=`<h3>The input field cannot be empty</h3>`;
}
else{
result.innerHTML=`<h3>Please enter a valid country name.</h3>`;
}
});
});
document.addEventListener("click", function(e) {
if (e.target && e.target.id === "clear-btn") {
result.innerHTML = "";
}
});