mirror of
https://github.com/wezm/wezm.net.git
synced 2024-12-18 18:29:54 +00:00
24 lines
838 B
JavaScript
24 lines
838 B
JavaScript
const select = document.getElementById('forex');
|
|
select.addEventListener('change', updatePrices);
|
|
// Trigger initial conversion (Firefox preserves selected option across refresh
|
|
updatePrices.call(select);
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
}
|
|
}
|