Alright so im trying to write a simple temperature conversion but i can't seem to get it to work 100%. My problem seems to be that I want to check to see which field has had a number entered in it, and then run the correct conversion function accordingly. I'm rather new with JS so... I'm probably misusing isFinite, but I'm not sure how else to do it.
Any help would be great. Thanks! =]
<script type="text/javascript">
var fahrenheit= Number(document.temp_conv.fahr.value);
var celsius= Number(document.temp_conv.cels.value);
var kelvin= Number(document.temp_conv.kelv.value);
if(isFinite(fahrenheit)){
function conversion() {
var f= Number(document.temp_conv.fahr.value);
var c= Number(document.temp_conv.cels.value);
var k= Number(document.temp_conv.kelv.value);
var f1= 5/9;
var s1= f-32;
var cels= f1*s1
document.temp_conv.cels.value = cels;
var kelv= cels + 273
document.temp_conv.kelv.value = kelv;
}
}
if(isFinite(celsius)){
function conversion() {
var f= Number(document.temp_conv.fahr.value);
var c= Number(document.temp_conv.cels.value);
var k= Number(document.temp_conv.kelv.value);
var fahr= 1.8*c+32
document.temp_conv.fahr.value = fahr;
var kelv= c + 273
document.temp_conv.kelv.value = kelv;
}
}
if(isFinite(kelvin)){
function conversion() {
var f= Number(document.temp_conv.fahr.value);
var c= Number(document.temp_conv.cels.value);
var k= Number(document.temp_conv.kelv.value);
var cels= k - 273
document.temp_conv.kelv.value = kelv;
var fahr= 1.8*cels+32
document.temp_conv.fahr.value = fahr;
}
}
</script>
</head>
<body>
<form name="temp_conv">
Fahrenheit<br />
<input type="text" name="fahr" id="fahr" value="" /><br />
Celsius<br />
<input type="text" name="cels" id="cels" value="" /><br />
Kelvin<br />
<input type="text" name="kelv" id="kelv" value="" />
<input type="button" value="Convert" onclick="conversion()" />
</form>