Samuel Duffy
Samuel Duffy

Reputation: 3

Trouble with html input box

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

Answers (3)

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7668

  • You are not getting value of select
  • You are not comparing select value in if condition, make it as select == 1 and select == 2

Complete solution here

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

Mohit Bhardwaj
Mohit Bhardwaj

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

Niko
Niko

Reputation: 6269

You are not comparing select,you are setting it.

   if (select == 1) {
       document.getElementById("answer").innerHTML = ...
   } else if (select == 2) {
  • = -> setting a value
  • == -> comparing

Upvotes: 1

Related Questions