diff --git a/fetch-api-form.html b/fetch-api-form.html new file mode 100644 index 0000000..600836e --- /dev/null +++ b/fetch-api-form.html @@ -0,0 +1,17 @@ + + + + + Form Fetch API + + + +

Inserisci una valuta per ottenere i tassi di cambio con l'Euro

+
+ + +
+
+

+ + diff --git a/fetch-form-api.js b/fetch-form-api.js new file mode 100644 index 0000000..387febc --- /dev/null +++ b/fetch-form-api.js @@ -0,0 +1,30 @@ +function getJSON(response){ + let data = response.json(); + return data +} + +function getCurrencyRate(currency){ + console.log(currency); + let url = "https://api.exchangeratesapi.io/latest?symbols=" + currency; + fetch(url) + .then(getJSON) + .then(data => { + console.log("Data : " ,data) + let rate = Object.values(data.rates)[0]; + document.querySelector("#exchange-result").innerHTML = `1.00 Euro corrisponde a ${rate} ${currency}`; + }) + .catch( err => { + // console.log("Errore : ", err) + document.querySelector("#exchange-result").innerHTML = `Errore ${err}`; + }) +} +document.addEventListener("DOMContentLoaded", () =>{ + document.querySelector("#form").onsubmit = event =>{ + event.preventDefault(); + + const currency = document.querySelector("#currency").value; + getCurrencyRate(currency) + + document.querySelector("#currency").value = ""; + } +})