Click to See Complete Forum and Search --> : Radio buttons calculcator help


XLR3
07-28-2008, 02:32 PM
Currently I'm working on a currency calculator to perform the calculation of converting American to Euro and Euro to American. My biggest issue is how to get the If statements to work with Javascript. Currently this is my code:


<FORM NAME="cform">
<Input type = radio Name = r1 Value = "American">American
<Input type = radio Name = r1 Value = "Euro">Euro
</FORM>

<head>
<title>Money Exchange</title>
<script language="JavaScript">
<!--
function convert() {
var amt = parseFloat(exchange.amount.value);
var euro = Math.round((amt * .7) * 100) / 100;
document.write(exchange.amount.value + " dollars is " + euro + " Euros");
}
{
var amt = parseFloat(exchange.amount.value);
var american = Math.round((amt * 1.568) * 100) / 100;
document.write(exchange.amount.value + " euros is " + american + " Dollars ");
//-->
</script>
</head>
<body>
<form name="exchange" action="javascript:convert()" method="post">
Amount
<input type="text" name="amount"><br/>
<input type="submit" value="Submit Form">
</form>

I have been Googling many tutorials but I'm still not sure where to begin. Any helps or tips would be appreciated. Thank you.

vinays84
07-28-2008, 04:34 PM
<form name="cform" id="cform">
<input type="radio" name="r1" id="amerRadio" value="American" onchange="document.getElementById('conv').value='Convert to Euro'" checked>
<label for="amerRadio">American</label>
<input type="radio" name="r1" id="euroRadio" "value="Euro" onchange="document.getElementById('conv').value='Convert to American'">
<label for="euroRadio">Euro</label>
</form>

Amount:
<input type="text" id="amount">
<input type="button" id="conv" value="Convert to Euro" onclick="convert()" >

<br><br>
<div id="resultDiv"></div>

<script type="text/javascript">
function convert() {
var value = document.getElementById("amount").value;
if ( isNaN(value) ) {
alert( 'Please enter a number.' );
return;
} else {
value=Math.round(Number(value)*1000)/1000;
}
var isAmerican=document.forms.cform.r1[0].checked;
var result = isAmerican ? value*1.5735 : value/1.5735;
result=Math.round(result*1000)/1000;

var resultsDiv = document.getElementById('resultDiv');
if (isAmerican) {
resultsDiv.innerHTML = value + " euros is " + result + " Euros.";
} else {
resultsDiv.innerHTML = value + " dollars is " + result + " Dollars.";
}
}
</script>