1
0
Fork 1
mirror of https://github.com/wezm/wezm.net.git synced 2024-12-24 04:59:53 +00:00
wezm.net/v2/static/js/forex.js

25 lines
838 B
JavaScript
Raw Normal View History

2024-07-16 00:32:42 +00:00
const select = document.getElementById('forex');
select.addEventListener('change', updatePrices);
// Trigger initial conversion (Firefox preserves selected option across refresh
updatePrices.call(select);
2024-07-15 11:03:52 +00:00
/**
* @this {HTMLSelectElement}
*/
function updatePrices() {
const select = this;
const option = select.options[select.selectedIndex];
const ccy = option.value;
const rate = parseFloat(option.dataset.price);
const formatter = new Intl.NumberFormat(navigator.languages, { style: 'currency', currency: ccy });
const prices = document.querySelectorAll('.money');
for (let i = 0; i < prices.length; i++) {
const el = prices[i];
let converted = parseFloat(el.dataset.amount) * rate;
if (!isNaN(converted)) {
el.innerText = formatter.format(converted);
}
}
}