From c6f96fbf15cf0f54e4a4b6f226ec9844cd31a4b7 Mon Sep 17 00:00:00 2001 From: Mick877 Date: Sun, 12 Jan 2020 14:50:24 +0100 Subject: [PATCH] Uploaded File --- fetch-api-form.html | 17 +++++++++++++++++ fetch-form-api.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 fetch-api-form.html create mode 100644 fetch-form-api.js 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 = ""; + } +})