Update script.js
Browse files
script.js
CHANGED
@@ -8,39 +8,36 @@ const resultDiv = document.getElementById('result');
|
|
8 |
// Status konversi otomatis
|
9 |
let autoConvert = false;
|
10 |
|
11 |
-
// Tombol Convert
|
12 |
convertButton.addEventListener('click', () => {
|
13 |
autoConvert = true; // Aktifkan auto-convert
|
14 |
-
convertCurrency(); // Lakukan konversi
|
15 |
hideConvertButton(); // Sembunyikan tombol Convert
|
16 |
});
|
17 |
|
18 |
// Swap mata uang
|
19 |
-
swapButton.addEventListener('click',
|
20 |
const temp = fromCurrency.value;
|
21 |
fromCurrency.value = toCurrency.value;
|
22 |
toCurrency.value = temp;
|
23 |
|
24 |
-
// Lakukan konversi langsung jika autoConvert aktif
|
25 |
if (autoConvert) {
|
26 |
-
convertCurrency();
|
27 |
} else {
|
28 |
-
clearResult(); // Hapus hasil jika
|
29 |
}
|
30 |
});
|
31 |
|
32 |
-
// Pantau perubahan input
|
33 |
[amountInput, fromCurrency, toCurrency].forEach((element) => {
|
34 |
element.addEventListener('input', () => {
|
35 |
if (autoConvert) {
|
36 |
-
convertCurrency(); // Konversi otomatis
|
37 |
}
|
38 |
});
|
39 |
|
40 |
element.addEventListener('change', () => {
|
41 |
-
|
42 |
-
showConvertButton(); // Tampilkan tombol Convert kembali
|
43 |
-
clearResult(); // Hapus hasil sebelumnya
|
44 |
});
|
45 |
});
|
46 |
|
@@ -89,3 +86,10 @@ function showConvertButton() {
|
|
89 |
function clearResult() {
|
90 |
resultDiv.innerHTML = '';
|
91 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
// Status konversi otomatis
|
9 |
let autoConvert = false;
|
10 |
|
11 |
+
// Tombol Convert ditekan
|
12 |
convertButton.addEventListener('click', () => {
|
13 |
autoConvert = true; // Aktifkan auto-convert
|
14 |
+
convertCurrency(); // Lakukan konversi
|
15 |
hideConvertButton(); // Sembunyikan tombol Convert
|
16 |
});
|
17 |
|
18 |
// Swap mata uang
|
19 |
+
swapButton.addEventListener('click', () => {
|
20 |
const temp = fromCurrency.value;
|
21 |
fromCurrency.value = toCurrency.value;
|
22 |
toCurrency.value = temp;
|
23 |
|
|
|
24 |
if (autoConvert) {
|
25 |
+
convertCurrency(); // Lakukan konversi otomatis setelah swap
|
26 |
} else {
|
27 |
+
clearResult(); // Hapus hasil jika auto-convert tidak aktif
|
28 |
}
|
29 |
});
|
30 |
|
31 |
+
// Pantau perubahan pada input dan dropdown
|
32 |
[amountInput, fromCurrency, toCurrency].forEach((element) => {
|
33 |
element.addEventListener('input', () => {
|
34 |
if (autoConvert) {
|
35 |
+
convertCurrency(); // Konversi otomatis saat ada input
|
36 |
}
|
37 |
});
|
38 |
|
39 |
element.addEventListener('change', () => {
|
40 |
+
resetAutoConvert(); // Reset auto-convert saat dropdown berubah
|
|
|
|
|
41 |
});
|
42 |
});
|
43 |
|
|
|
86 |
function clearResult() {
|
87 |
resultDiv.innerHTML = '';
|
88 |
}
|
89 |
+
|
90 |
+
// Fungsi untuk mereset status auto-convert
|
91 |
+
function resetAutoConvert() {
|
92 |
+
autoConvert = false; // Nonaktifkan auto-convert
|
93 |
+
showConvertButton(); // Tampilkan kembali tombol Convert
|
94 |
+
clearResult(); // Hapus hasil sebelumnya
|
95 |
+
}
|