Reputation: 3
I'm making a basic usd converter for practice in html/javascript. However when I select the euro option it does the same as for the peso option.
<html>
<head>
<title>Currency Converter</title>
<style>
</style>
</head>
<body>
<input id="amount"> </input>
<p>usd Contverted to</p>
<p class="output"> </p>
<select id="select"> <option value="1">Peso's</option> <option value="2">Euro's</option> </select>
<p id="answer"> is </p>
<input type="submit" value="Submit" onclick="run()">
<script>
function run() {
var Amount = document.getElementById("amount").value;
if (select = 1) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 16.39 + " =-=-=";
} else if (select = 2) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 0.9 + " =-=-=";
} else {
}
</script>
</body>
</html>
Upvotes: 0
Views: 48
Reputation: 7668
Change your javaScript function as below
function run() {
var Amount=document.getElementById("amount").value;
var select=document.getElementById("select").value;
if (select == 1) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 16.39 + " =-=-=";
} else if (select == 2) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 0.9 + " =-=-=";
} else {
}
}
Upvotes: 0
Reputation: 10083
it should be select == 1
select=1
will always return true, because this way you are simply assigning a value, not checking for equality
Upvotes: 1
Reputation: 6269
You are not comparing select,you are setting it.
if (select == 1) {
document.getElementById("answer").innerHTML = ...
} else if (select == 2) {
Upvotes: 1