i’m trying to display a value of 1 in the placeholder of my input when i select a currency, but instead of 1 i see the exchange rate of the selected currency with USD, for example if i select EUR instead of 1 i see 0.93 (1 USD = 0.93 EUR). Any suggestion how to fix it? The issue is only in the placeholder of the currency that i’ve choose, in the second input box i see the correct exchange rate between the selected currency and the other currency.
let selectedCurrency1 = "USD";
let selectedCurrency2 = "EUR";
let exchangeRate: number;
function handleSelectChange(event: any, currency: any) {
if (currency === 1) {
selectedCurrency1 = event.target.value;
if (input1) {
input1.placeholder = "1";
}
} else if (currency === 2) {
selectedCurrency2 = event.target.value;
if (input2) {
input2.placeholder = "1";
}
}
updateExchangeRate();
}
function updateExchangeRate() {
const rate1 = exchangeRates[selectedCurrency1];
const rate2 = exchangeRates[selectedCurrency2];
const exchangeRate = rate2 / rate1;
const input1 = document.getElementById("rate1") as HTMLInputElement;
const input2 = document.getElementById("rate2") as HTMLInputElement;
if (input1 && input2 && exchangeRate) {
input1.placeholder = `${(1 / exchangeRate).toFixed(3)}`;
input2.placeholder = `${(1 * exchangeRate).toFixed(3)}`;
}
}
and this is the block with the input
{#each Object.entries(exchangeRates) as [key, value], index}
{#if index === 0}
<div class="input-box">
<input type="number" min="0" id="rate1" placeholder={exchangeRates[selectedCurrency1]}/>
<select id="option1" on:change={(event) => handleSelectChange(event, 1)}>
{#each Object.entries(exchangeRates) as [key, value]}
{#if key === selectedCurrency1}
<option value={key} selected>{key}</option>
{:else}
<option value={key}>{key}</option>
{/if}
{/each}
</select>
</div>
{/if}
{/each}