<script type=
"text/javascript"
>
function
isNumber(x)
{
return
( (
typeof
x ===
typeof
1) && (
null
!== x) && isFinite(x) );
}
function
Round(number, digits)
{
return
Math.round(number * Math.pow(10,digits)) / Math.pow(10, digits);
}
function
CelsiusToFahrenheit(celsius, fahrenheit)
{
var
num = celsius.value;
if
(num ==
""
|| !isNumber(Number(num)))
alert(
"Please type a number"
);
else
fahrenheit.value=Round((9/5)*num+32, 2);
}
function
FahrenheitToCelsius(fahrenheit, celsius)
{
var
num = fahrenheit.value;
if
(num ==
""
|| !isNumber(Number(num)))
alert(
"Please type a number"
);
else
celsius.value=Round((5/9)*(num-32), 2);
}
</script>
<table>
<tr>
<td>Celsius:</td>
<td><input type=
"text"
name=
"celsius_field"
id=
"celsius_field"
style=
"width:100px"
/></td>
<td><input type=
"button"
value=
"To Fahrenheit"
onclick=
"CelsiusToFahrenheit($('celsius_field'), $('fahrenheit_field'))"
/></td>
</tr>
<tr>
<td>Fahrenheit:</td>
<td><input type=
"text"
name=
"fahrenheit_field"
id=
"fahrenheit_field"
style=
"width:100px"
/></td>
<td><input type=
"button"
value=
"To Celsius"
onclick=
"FahrenheitToCelsius($('fahrenheit_field'), $('celsius_field'))"
/></td>
</tr>
</table>
Recent Comments