Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NEED HELP with simple JAVASCRIPT and HTML

    Hi everyone,

    I am new to javascript, i have not read any books, but i am trying to figure out the language on the go. I am stuck with a simple program that i am trying to create for a class project. Any suggestions or help would much appreciated.

    My code is very simple in which i would like to convert a number value into different format of temperature.

    My code is listed below, it is not working in the browser. Help me fix it.
    *********************************************************

    <!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>
    <meta content="text/html" http-equiv="Content-Type" />
    <title>E6B temp conversion</title>
    <script type="text/javascript">
    function ConvertTempToC(TempConversionC)
    {
    var temperature = TempConversionC.F.value;
    if (temperature.length > 0)
    {
    TempConversionC.TempC.value = Math.round(5/9 * (temperature - 32));
    document.write(TempConversionC.TempC.value);
    }
    }
    </script>
    </head>


    <body>
    <b>Fahrenheit to Celsius</b> <br/>
    <input size="6" name="F"/> <br/>
    <input onclick="ConvertTempToC(this.form)" value="Calculate Celsius" type="button" name="Submit11"/> <br/>
    <input readonly="readonly" size="6" name="TempC"/>

    </body>
    </html>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    have you looked at your code using an error console?

    you are sending this.form in your onclick:

    Code:
    onclick="ConvertTempToC(this.form)"
    but there is no form.

    wrap your inputs in <form> tags

    (and get rid of the document.write statement - it is worse than useless)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,458
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Most web sites use antiquated JavaScript either because the person who wrote it hasn't had the time to rewrite it or they don't realise how antiquated that it is.

    There is almost nothing in the script you wrote that belongs in JavaScript written for 2005 and later.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    var temperature = TempConversionC.F.value;
    if (temperature.length > 0)

    You need to check that that the value input by the user is a number (within a sensible range).
    The string Mickey Mouse has a length > 0.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •