-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (67 loc) · 2.66 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
// Catching all needed element with DOM
var capital = document.getElementById("capital");
var flag = document.querySelector("#flag-containe img");
var population = document.getElementById("population");
var currencies = document.getElementById("currencies");
var subregion = document.getElementById("subregion");
var region = document.getElementById("region");
var dialingCode = document.getElementById("dialing-code");
var flagImage = document.getElementById("flagImage");
// array use to stock my global object
var arrayGlobal = [];
function ajaxGet(url, callback) {
// HTTP requets
var req = new XMLHttpRequest();
// open the request and past HTTP method name and the resource as parameters
req.open("GET", url);
req.addEventListener("load", function () {
// if request succes
if (req.status >= 200 && req.status < 400) {
// Appelle la fonction callback en lui passant la réponse de la requête
callback(req.responseText);
} else {
console.error(req.status + " " + req.statusText + " " + url);
}
});
// listen for error
req.addEventListener("error", function () {
console.error("Erreur réseau avec l'URL " + url);
});
req.send(null);
}
ajaxGet("https://restcountries.eu/rest/v2/all", function (response) {
var countries = JSON.parse(response);
// myArrayOfCountry.push(countries);
countries.forEach((countrie) => {
var myArrayOfCountry = countrie.name;
arrayGlobal.push(countrie)
// add <option> to <select>
var selectList = document.createElement("option");
document.getElementById("countries").appendChild(selectList);
//Create and append the options
for (var i = 0; i < myArrayOfCountry.length; i++) {
var option = document.createElement("option");
option.value = myArrayOfCountry[i];
option.text = myArrayOfCountry[i];
selectList.appendChild(option);
}
});
});
function selectChange() {
var imageFlag = document.getElementById("flagImage");
imageFlag.style.visibility = "visible";
var selectCountry = document.getElementById("countries").value;
// finc the input in arrayGlobal
const found = arrayGlobal.find(element => element.name == selectCountry);
// country Info
capital.textContent = found.capital;
population.textContent = found.population;
currencies.textContent = found.currencies[0].symbol + " - " + found.currencies[0].code + ", " + found.currencies[0].name;
subregion.textContent = found.subregion;
region.textContent = found.region;
dialingCode.textContent = found.callingCodes;
// flag image
var flagUrl = found.flag
flagImage.setAttribute("src", flagUrl);
// document.body.style.backgroundImage = "url('img_tree.png')";
}