JavaScript Convert the Temperature Code

  1. <html>
  2. <head>
  3. <title>Convert the Temperature</title>
  4. <script type="text/javascript">
  5. //This Code was created by David A. and Matt I. on July 28, 2008
  6. //You may use this code freely
  7. //you may edit thos code however you wish
  8. //however please leave in who it was created by and the time
  9. //this would be apprciated.
  10. //Enjoy!
  11.  
  12. var theAlert = "Please enter a valid number ";
  13. var fahren;
  14. var convert;
  15. var cent;
  16.  
  17. function convertToCent_onchange()
  18. {
  19.  
  20.     if(isNaN(document.form1.fahren.value) == true || document.form1.fahren.value == "")
  21.     {
  22.         alert(theAlert + "in the fahrenheit box");
  23.         document.form1.fahren.value = 32;
  24.     }
  25.  
  26.     var fahren = document.form1.fahren.value;
  27.     var convert = 5/9 * (fahren - 32);
  28.     document.form1.cent.value = convert;
  29. }  
  30.  
  31.  
  32. function convertToFahren_onchange()
  33. {
  34.     if(isNaN(document.form1.cent.value) == true || document.form1.cent.value == "")
  35.     {
  36.         alert(theAlert + "in the celsius box");
  37.         document.form1.cent.value = 0;
  38.     }
  39.    
  40.     var cent = document.form1.cent.value
  41.     var convert = (9/5 * cent) + 32;
  42.     document.form1.fahren.value = convert;
  43. }
  44. </script>
  45. </head>
  46. <body>
  47.  
  48. <h2>Convert the temperature</h2>
  49. <form name="form1">
  50. <table style="width: 306px;"><tr><td>
  51. <p>Celcius</p></td><td>Fahrenheit</td></tr>
  52. <tr><td>
  53. <input type="text" name="cent" value="0" onchange="convertToFahren_onchange()" bgcolor="red">
  54. </td><td>
  55. <input type="text" name="fahren" value="32" onchange="convertToCent_onchange()">
  56. </td></tr>
  57. <tr><td colspan="2" style="text-align: center;">
  58. <input type="button" value="Convert" name="convert">
  59. </td></tr></table>
  60. </form>
  61.  
  62. </body>
  63. </html>