-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (49 loc) · 2.09 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
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
const currencyEl_one = document.getElementById('currency-one');
const amountEl_one = document.getElementById('amount-one');
const currencyEl_two = document.getElementById('currency-two');
const amountEl_two = document.getElementById('amount-two');
const rateEl = document.getElementById('rate');
const swap = document.getElementById('swap');
// Fetch exchange rates and update the DOM
function calculate() {
const currency_one = currencyEl_one.value;
const currency_two = currencyEl_two.value;
fetch(`https://api.exchangeratesapi.io/latest?base=${currency_one}`)
.then(res => res.json())
.then(data => {
// console.log(data);
const rate = (data.rates[currency_two]).toFixed(3);
rateEl.innerHTML = `1 ${currency_one} = ${rate} ${currency_two}`;
amountEl_two.value = (amountEl_one.value * rate).toFixed(2);
});
}
// Event listeners
currencyEl_one.addEventListener('change', calculate);
amountEl_one.addEventListener('input', calculate);
currencyEl_two.addEventListener('change', calculate);
amountEl_two.addEventListener('input', calculate);
swap.addEventListener('click', () => {
const temp = currencyEl_one.value;
currencyEl_one.value = currencyEl_two.value;
currencyEl_two.value = temp;
calculate();
});
calculate();
// Mobile menu
var inicio = true;
function mobileBtn() {
if (inicio) {
var closeBtn = document.querySelector('.menu');
closeBtn.className = 'openBtn';
closeBtn.innerHTML = '<span class="iconify" data-inline="false" data-icon="ic:round-menu"></span>';
document.getElementById('display-mob').style.height = '0';
inicio = false;
} else {
var burgerBtn = document.querySelector('.openBtn');
burgerBtn.className = 'menu';
burgerBtn.innerHTML = '<span class="iconify" data-inline="false" data-icon="ic:round-close"></span>'
document.getElementById('display-mob').style.height = '120px';
inicio = true;
}
}
mobileBtn();