-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (51 loc) · 1.87 KB
/
script.js
File metadata and controls
61 lines (51 loc) · 1.87 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
let Searchbtn = document.querySelector('#search')
let container = document.querySelector('.main-container-country')
let html = ``
function datafetch() {
let SearchCountry = document.querySelector('#search-country')
if (SearchCountry.value.length < 3) {
alert('Please Enter a Valid name of Country')
} else {
let dataresponse = fetch(
`https://restcountries.com/v3.1/name/${SearchCountry.value}`,
)
dataresponse
.then((responsedata) => {
return responsedata.json()
})
.then((predata) => {
const [realdata] = predata
console.log(realdata)
html = `
<div class="country-information">
<div class="country">
<div class="country-flag">
<figure>
<img src=${realdata.flags.png} alt="">
</figure>
</div>
<div class="country-details">
<div class="details-hold"><span>🕋</span><span style="margin:0px 8px">Country Name: </span> <span class="country-name">${realdata.name.common}</span></div>
<div class="details-hold"><span>🏛</span><span style="margin:0px 8px">Capital City: </span> <span class="capital-name">${realdata.capital[0]}</span></div>
<div class="details-hold" style="margin:10px 0px"><span>👩🏾🤝🧑🏼</span> <span style="">Population:</span><span class="population-number">${realdata.population}</span></div>
</div>
</div>
</div>
`
container.insertAdjacentHTML('afterbegin', html)
SearchCountry.value = ''
})
.catch((err) => {
alert("Page not found"+err);
})
}
}
Searchbtn.addEventListener('click', () => {
datafetch()
})
let body = document.querySelector('body')
body.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
datafetch()
}
})