JavaScript_Fetch_API/fetch-form-api.js

31 lines
977 B
JavaScript
Raw Normal View History

2020-01-12 13:50:24 +00:00
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 = "";
}
})