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 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts

    javascript not working

    When i run this it says NAN can anyone help I'm suppose to use parseInt() but it still says NAN

    <!DOCTYPE HTML>
    <html>
    <head>
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    function cool(w,h){
    var result = calcBMI(w,h);
    document.mass.final.value = result; // sending to text box final
    }
    function calcBMI(w,h){
    var temp = ( w * 703 / ( h*h ) );
    return temp;
    }
    </script>
    </head>
    <body>
    <h1>Body Mass Index</h1>
    <form name="mass" action="" method="get">
    <p>
    Weight<input type="text" value="0" name="w" onchange="cool(document.mass.w.value)">
    <br>
    Height<input type="text" value="0" name="h" onchange="cool(document.mass.h.value)">
    <br>
    BMI<input type="text" name="final"> <!--should be receiving value-->
    <br>
    <input type="button" name="calc" value="Calculate" onclick="calcBMI()">
    </p>
    </form>

    </body>
    </html>


    Thanks.
    Last edited by CodyJava; 10-12-2012 at 08:31 PM.

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You call a function that produces no output and just returns a value.
    You're doing nothing with that value.

  • Users who have thanked Logic Ali for this post:

    CodyJava (10-12-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Hi thanks for the reply i updated it and am now having trouble with NAN.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Why didn't you like yesterday's answer?

    Anyway, it is OBVIOUS:

    Code:
    function cool( w, h ){
    but then
    Code:
    <input type="text" value="0" name="w" 
        onchange="cool( document.mass.w.value )">
    Your function requres that you pass in *TWO* values.

    You only pass it ONE value when you call it.

    Go look at what I gave you yesterday: The right answer is to have a function that requires ZERO arguments and you call it with ZERO arguments. Then the function itself goes and gets both needed values.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And you also COMPLETELY IGNORED the answer Philip gave you here:
    Need help with using text boxes and linking them back to java script

    Why do you ask questions if you don't want to read and learn from the answers you get?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts
    I didn't like yesterdays because it used if statements and things I don't yet understand. I would rather understand it than just copy someones code. Your saying i need to pass 2 values but aren't I doing that with the onchange. Then when you use the onclick it should calculate them.

  • #7
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Weight<input type="text" value="0" name="w">
    <br>
    Height<input type="text" value="0" name="h">
    <br>
    BMI<input type="text" name="final"> <!--should be receiving value-->
    <br>
    <input type="button" name="calc" value="Calculate" onclick="cool(document.mass.w.value,document.mass.h.value)">

    Don't have the fields call the function on change or blur, have the button call the cool() function on click. The cool() function is already calling the calcBMI() function and getting a return value.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    CodyJava (10-12-2012)

  • #8
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Wow thank you Wolfshade I was looking for a simple answer not a high level answer that I wouldn't understand. Your a life saver

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    *SIGH*

    If you didn't want an answer that used onchange then why did you write the code to use onchange?

    *DOUBLE SIGH*

    As long as you were using onchange you had to protect against no height value yet being entered (because then you will be trying to divide by zero).

    Even as it is, Wolfshade's answer doesn't protect against somebody entering zero for the height.

    Anyway, from now on, if you don't like an answer, say so. Don't keep starting new threads that ask the same question over and over.

    And, finally, none of the answers you got were "high level". At *MOST*, they were one or two steps up from where you are now.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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