PDA

View Full Version : javascript help urgently needed



neil275
04-02-2011, 01:59 PM
Hi All,
I have started learning how to write javascript at a local college but i'm having trouble learning how to use radio buttons and return the info.
My question is this.
Below i have the code i have had to write.
Essentially i have to have a text field that has entered into it a temperature.
Then a radio button is selected.
If the button selected is celsius it converts to fahrenheit and vice a versa.
I am having a lot of trouble working this out from text book.

Hoping dearly someone can help me.
[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script src="JscriptUTILSa.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">

function submitIt()
{
var temperature = document.getElementById("tempin");
var answer;
var len = f.temp.length
var type
for (i = 0; i <len; i++) <br />
{
if (f.temp[i].checked) <br />
{
type = f.temp[i].value;
}
}
if (type == Cel)
{
answer = 5/9 * (temperature - 32 );
}

else if (type == Fah)
{
answer = 9* temperature/5 + 32 ;
}
else
{
alert ("Try again");
}
document.getElementById("result").innerHTML = answer;
}
</script>
</head>
<body>


<form action="" method="POST" name="val1" onSubmit="return submitIt()">
<table width="29%" border="0" align="center" cellpadding="3">

<tr>
<td width="40%"><h4><strong>Conversion of one temp to another.</strong><br />
1. Input a temperature <br />
2. Select celsius to convert to fahrenheit<br />
3. Select
fahrenheit to convert to celsius</h4></td>
<td width="40%"><h2>&nbsp;</h2></td>
</tr>

<tr>
<td><label for="tempin">Temperature Input</label>
<input type="text" name="tempin" id="tempin" value="" /></td>
<td><div id="result">
</div></td>
</tr>
<tr>
<td colspan="3"> <label for="celSelect">Celsius</label>
<input type="radio" name="temptype" id ="celsius" value="Cel" />

<label for="farhrenheit">Fahrenheit</label>
<input type="radio" name="temptype" id="fahrenheit" value="Fah" />
</td>
</tr>
<tr>
<td colspan="4" align="right">
<input type="submit" name="Submit" value="Submit"> </td></td>
</tr>
</table>

</form>

</body>
</html>




[CODE]


Thank you.
Neil