Jump to content

Can't figure out the javascript

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Hello i am at the moment making a javascript temperature converter where the user keys in a number and can convert it to either Celsius or Fahrenheit by clicking on either one of the radio buttons. I'm not sure how to code it in so it displays this for the user.

Here is my code so far,


<!-- Temperature Converter -->

<html>

 <head>

  <title>Temperature Converter</title>

  <style type="text/css">

   body {

          background-color: #000000;

          color: #FFFFFF;

          text-align: center;

          font-size: 25;

        }

  </style>


  <script type="text/javascript">

    

    function convert()

  {


  if (document.frm.txtTemp.value == "")

         {

            alert("Please enter a temperature");

            document.frm.txtTemp.focus();

         }

         else if (document.frm.txtTemp.value != parseFloat(document.frm.txtTemp.value))

         {

            alert("Your temperature must be a number");

            document.frm.txtTemp.focus();

         }

         else if (document.frm.Cels.Faren.value == "")

         {

            alert("Please select one of the radio buttons");

            document.frm.Cels.Faren.focus();

         }


  }


  function clean()

       {

         //alert("in clean");

         document.frm.txtTemp.value = "";

         document.frm.txtTemp.value = "";

         document.frm.readonly.value = "";

         document.frm.txtTemp.focus();

         document.frm.txtTemp.focus();

       }


   </script>


 </head>

 <body onload="window.document.frm.txtTemp.focus()">


  <h1>Temperature Converter</h1>

  <form name="frm"> 

    <tr><td>Enter a temperature:  </td>

    <td><input type=text name="txtTemp" size=12"></td>

    <br /><br />

    <td>

    <input type="radio" name="Cels" value="Celsius" class="radio" /> Celsius<br />

    <input type="radio" name="Faren" value="Fahrenheit" class="radio" /> Fahrenheit

    </td>


    <br /><br />

    <input type="button" name="cmdConvert" value="Convert" onclick="convert()">

    	 

    <input type="button" name="cmdClear" value="Reset" onClick="clean()">

    <br /><br /><br />

    <input type=text name="readonly" size="12" disabled="READONLY" />

   </form>


 </body>

</html>


Many thanks to whoever can help me.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
else if (document.frm.Cels.Faren.value == "")
This is wrong.

You'll need something like:
else if (!(document.frm.Cels.checked || document.frm.Faren.checked))
         {
            alert("Please select one of the radio buttons");
            document.frm.Cels.Faren.focus();
         }
		 else 
		 {
			 if(document.frm.Cels.checked)
			 {
				var fahrenheit = parseFloat(document.frm.txtTemp.value);
				document.frm.readonly.value = (fahrenheit - 32 ) * (5/9);
			 }
			 if(document.frm.Faren.checked)
			 {
				var celcius = parseFloat(document.frm.txtTemp.value);
				document.frm.readonly.value = (celcius * 9/5 ) +32;
			 }
		}