-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (39 loc) · 1.56 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
'use strict';
const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector('.countries');
///////////////////////////////////////
////Old School AJAX - XMLHttpRequest format
const getCountryInfo = function(country){
const requestData = new XMLHttpRequest();
requestData.open('GET', `https://restcountries.eu/rest/v2/name/${country}`);
requestData.send(); //Async Call
//Once recieved -- listen for load and do something
requestData.addEventListener('load', function (e) {
//using destructuring to get the relevant objects
const [data] = JSON.parse(requestData.responseText);
console.log(data);
const html = `
<article class="country">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
data.population / 1000000
).toFixed(2)}M</p>
<p class="country__row"><span>🗣️</span>${data.languages[0].name}</p>
<p class="country__row"><span>💰</span>${data.currencies[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
});
}
getCountryInfo('pakistan');
getCountryInfo('saudi');
getCountryInfo('canada');
getCountryInfo('great');
getCountryInfo('uae');
getCountryInfo('ukraine');
getCountryInfo('zambia');