Reputation: 15
I'm new to Javascript, and I want to swap these two options when the exchange button is clicked Page
HTML
<div class="form-group col-4">
<button type="button" class="btn bg-dark text-white" id="exchange" style="margin-top: 23px; margin-left: 10px;">
<i class="fa fa-exchange" aria-hidden="true"></i>
</button>
</div>
Javascript
function swap() {
let inp = document.getElementById("inputCurrency").value;
let out = document.getElementById("outputCurrency").value;
document.getElementById("inputCurrency").value = out;
document.getElementById("outputCurrency").value = inp;
}
let amountInput = document.getElementById("amount");
let inputCurrency = document.getElementById("inputCurrency");
let outputCurrency = document.getElementById("outputCurrency");
let convertButton = document.getElementById('convertButton');
convertButton.addEventListener("click",convertCurrency);
let exchangeButton = document.getElementById("exchange");
exchangeButton.addEventListener("click",swap());
Upvotes: 0
Views: 201
Reputation: 18975
You need remove function call in addEventListener, it contains only function name
exchangeButton.addEventListener("click",swap);
function swap() {
let inp = document.getElementById("inputCurrency").value;
let out = document.getElementById("outputCurrency").value;
document.getElementById("inputCurrency").value = out;
document.getElementById("outputCurrency").value = inp;
}
let amountInput = document.getElementById("amount");
let inputCurrency = document.getElementById("inputCurrency");
let outputCurrency = document.getElementById("outputCurrency");
let convertButton = document.getElementById('convertButton');
//convertButton.addEventListener("click",convertCurrency);
let exchangeButton = document.getElementById("exchange");
exchangeButton.addEventListener("click",swap);
<select id="inputCurrency">
<option value="1">USD</option>
<option value="2">KRW</option>
</select>
<select id="outputCurrency">
<option value="1">USD</option>
<option value="2">KRW</option>
</select>
<input type="text" id="amount"/>
<div class="form-group col-4">
<button type="button" class="btn bg-dark text-white" id="exchange" style="margin-top: 23px; margin-left: 10px;"> Exchange
<i class="fa fa-exchange" aria-hidden="true"></i>
</button>
</div>
<button id="convertButton">Convert</button>
Upvotes: 0
Reputation: 8241
addEventListener
needs a function for second parameter. Add a callback function that calls swap()
function.
exchangeButton.addEventListener("click", e => swap());
Upvotes: 0