2

I have two dropdown select boxes and I want a textbox to update with a value based on the selected texts from the two boxes. If the first box selected is 'dollars' and the second is 'naira', I want the rate to be 100 and this value inputted in the textbox with the id of rate.

Unfortunately when my function executes I keep getting a value of undefined. Something is obviously wrong with my if statement but I can't figure it out.I need to do this with pure javascript, not jquery.

Here is my code:

<p>Select currency to send</p>  
<select id="fromCurrency">
<option value = "Dollars">Dollars</option>  
<option value = "Pounds">Pounds</option>
<option value = "Naira">Naira</option>
</select>

<p>Select currency to receive</p>
<select id="toCurrency">
<option value = "Naira">Naira</option>
<option value = "Dollars">Dollars</option>  
<option value = "Pounds">Pounds</option>

</select><br />
<label>Enter amount to send</label>
<input type="text" id="amount" name="amount"><br />
<button onclick ="getRate()">Get Rate</button><br />
<label>Rate:</label>
<input type="text" id="rate" name="rate"><br />
<label>Total:</label>
<input type="text" id="total" name="total"><br />



<script>

function getRate() {
var e = document.getElementById("fromCurrency");
var eSend = e.options[e.selectedIndex].value;
var i = document.getElementById('toCurrency');
var eReceive = i.options[i.selectedIndex].value;
var rate = document.getElementById('rate');

var dollars = {
'pounds':20,
'naira':15
};


if (eSend =='dollars' && eReceive =='naira') {
var rValue= (dollars['pounds']);
};  
rate.value = rValue;

};
</script>

Please advice. Thanks

EDIT: I tried to use a switch statement as I have about 6 different conditions, and this is what I tried but it didn't work.

var rValue = 0;

switch (rValue) {
case (eSend =='Dollars' && eReceive =='Naira'):
rValue= (dollars['naira']);
break;
case (eSend =='Dollars' && eReceive =='Pounds'):
rValue= (dollars['pounds']);
break;
}

rate.value = rValue;
3
  • if (eSend =='Dollars' && eReceive =='Naira') Commented Jun 27, 2013 at 19:28
  • rValue isn't defined unless the if fires. Commented Jun 27, 2013 at 19:28
  • and btw you can do just var eSend = e.value; and var eReceive = i.value; Commented Jun 27, 2013 at 19:30

1 Answer 1

5

JavaScript is case-sensitive, your if statement:

if (eSend =='dollars' && eReceive =='naira') {

is looking for dollars, not Dollars, capitalize to match your select values:

if (eSend =='Dollars' && eReceive =='Naira') {
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick answer. It worked! Please see additional information I added about switch statements.
Well your switch statement won't work because there are no cases of 0.
Ok so what would you suggest? When I remove var rValue = 0, I get an undefined error.
Probably just an if else statement. Since your rValue is 0, the matching case statement would be case 0: rValue = "entered case 0"; break;
Thank you so much. You've been a great help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.